我正在製作遊戲。我已經設計了一個Flash程序欄,並鏈接到AS 3 在主類(main_c.as)我分配一個變種的階段:addChild拋出異常
package {
import flash.display.MovieClip;
import flash.display.Stage;
public class main_c extends MovieClip {
static public var stageRef:Stage;
public var s:start_b;
public var bar:timer_bar;
public function main_c()
{
// constructor code
stageRef = stage;
s = new start_b();
addChild(s);
s.x = 260;
s.y = 225;
}
}
}
然後有一個start_b類是創建一個按鈕並單擊以激發第三個類(game.as)的構造函數。這裏是start_b的代碼:
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class start_b extends SimpleButton {
public var g:game;
public function start_b()
{
// constructor code
this.addEventListener(MouseEvent.CLICK, start_g);
}
public function start_g(e:MouseEvent):void
{
g = new game();
this.removeEventListener(MouseEvent.CLICK, start_g);
this.visible = false;
}
}
而在最後一節課我想的addChild參照階段的狀態欄,但是當我跑我得到錯誤 -
TypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()
這裏是第三類的代碼(game.as):
package{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import main_c;
public class game extends MovieClip {
public var points:Number;
public var ptw:Number;
public var time:Timer;
public var bar:timer_bar = new timer_bar();
public var cnt:main_c;
public function game()
{
//restartirane na igrata (nulirane)
main_c.stageRef.addChild(bar);
points = 0;
time = new Timer(50);
time.addEventListener(TimerEvent.TIMER, flow);
time.start();
trace("d");
}
public function flow(t:TimerEvent):void
{
//code
//bar.y++;
}
public function addPoints():void
{
//function code here
}
public function removePoints():void
{
//function code here
}
public function checkTime():void
{
//function code here
}
public function end():void
{
//function code here
}
}
}
如果你能幫助我,我將:-)感謝和美好的一天很高興!
如果有人有解決方案,請寫下:-) – Mariyan
看起來你已經有了一些很好的答案,但是關於好的編碼實踐只是一些注意事項:1)類名應該大寫並使用UpperCamelCase(與標準Flash庫類被命名,例如:MovieClip)。所以,你的'timer_bar'類應該被命名爲'TimerBar',你的'main_c'類應該被命名爲'MainC'等。2)通常希望使用比單個字符更可讀的人名。你的行's = new start_b();'會比'startBtn = new StartButton();'更具可讀性。無論如何,只是一些指針:) – Ian