我正在制作游戏。我已经设计了一个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