2011-11-30 69 views
0

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

} 

} 

如果你能帮助我,我将:-)感谢和美好的一天很高兴!

+0

如果有人有解决方案,请写下:-) – Mariyan

+2

看起来你已经有了一些很好的答案,但是关于好的编码实践只是一些注意事项:1)类名应该大写并使用UpperCamelCase(与标准Flash库类被命名,例如:MovieClip)。所以,你的'timer_bar'类应该被命名为'TimerBar',你的'main_c'类应该被命名为'MainC'等。2)通常希望使用比单个字符更可读的人名。你的行's = new start_b();'会比'startBtn = new StartButton();'更具可读性。无论如何,只是一些指针:) – Ian

回答

0

你需要检查你的舞台已准备就绪:

Main_c /构造:

public function main_c() 
    { 
    if (stage) 
    { 
     init(); 
    } 
    else 
    { 
     addEventListener(Event.ADDED_TO_STAGE, init); 
    } 
    } 

Main_c /初始化:

private function init(e:Event = null):void 
{ 
    removeEventListener(Event.ADDED_TO_STAGE, init); 

    stageRef = stage; 
    s = new start_b(); 
    addChild(s); 
    s.x = 260; 
    s.y = 225; 
} 
+0

非常感谢,这项工程;-) – Mariyan

+0

惠康:) ..并永远标记接受的答案,如果这是对你的问题的正确答案..和其他好的答案,你可以给他们投票。顺便说一句,伊恩在评论中有一些很好的提示。 – Jarno

0

线索#1:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()

这意味着某些obj game构造函数中的ect为null,但您试图访问该对象的成员函数或属性。

打破它:

main_c.stageRef.addChild(bar); 
points = 0; 
time = new Timer(50); 
time.addEventListener(TimerEvent.TIMER, flow); 
time.start(); 
trace("d"); 

在这里的错误的唯一可能的原因是第一线

main_c.stageRef.addChild(bar); 

因此,解决这将是看是否main_c.stageRef是空的,采取相应的行动

我解决了这个:重新定义游戏类的构造函数:

callLater方法

在一个不相关的音符10

public function game() { 
    init(); 
} 

public function init() { 
    if(main_c.stageRef) { 
     //restartirane na igrata (nulirane) 
     main_c.stageRef.addChild(bar); 
     points = 0; 
     time = new Timer(50); 
     time.addEventListener(TimerEvent.TIMER, flow); 
     time.start(); 
     trace("d"); 
    } else { 
     callLater(init); 
    } 
} 

文档,ActionScript类的名字开始与约定大写字母。这有助于将它们与以小写字母开头的实例名称区分开来。

+0

非常感谢,我会牢记:-)你真的帮助我,祝你有美好的一天! – Mariyan