2013-04-26 58 views
0


我开始在闪光灯的拍摄时间,达到游戏中的教程工作,我做完了Asgamer Shoot em upp Game
现在我开始创建新的.fla文件是主菜单和我有一个播放按钮,所以当我按下它将加载.swf(游戏瑞士法郎),但当我按下按钮,我得到以下错误。类型错误:错误#1009 ActionScript 3的加载外部SWF

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
-at com.senocular.utils::KeyObject/construct() 
-at com.senocular.utils::KeyObject() 
-at com.actionscript.Ergasia::Ship() 
-at com.actionscript.Ergasia::Engine() 





public function Engine() : void { 
    if(stage) { 
     initialize(); 
    } else { 
     addEventListener(Event.ADDED_TO_STAGE,initialize); 
    } 
} 

    private function initialize(e:Event = null):void { 
     removeEventListener(Event.ADDED_TO_STAGE,initialize); 
     // here goes the code that's currently in Engine constructor 
    } 

编辑:感谢Vi蛇解决这个问题!

+0

很可能你的游戏SWF是用'stage'创建的,而在这个内容中它的'stage'属性是空的,因为它已经加载了,但是没有添加到任何地方。因此,无论您在游戏SWF中处理stage的哪个位置,都应创建一个Event.ADDED_TO_STAGE侦听器,并将所有访问阶段的代码放在那里,也不允许在stage为空时继续执行。此外,启动加载SWF还不够,您需要等到它完成加载。 – Vesper 2013-04-26 05:34:00

+0

首先感谢您的快速回答, ,因为我在as3中的初学者,我在哪里放置Event.ADDED_TO_STAGE? – 2013-04-26 05:41:13

+0

这取决于代码的哪些部分,以及哪些类在其构造函数中引用阶段。至少它是'船'级。修改你的整个代码库的构造函数,它们会在内部调用'stage.something',如果你让它们的话会给你一个1009的错误。相反,打一个'if(stage)init(null); else addEventListener(Event.ADDED_TO_STAGE,init);'在每个结尾处的行,并实现'function init(e:Event):void {removeEventListener(Event.ADDED_TO_STAGE,init); ......}这将会处理你的阶级在舞台上需要的任何东西。 – Vesper 2013-04-26 05:47:14

回答

0

更改Engine构造函数如下:

public function Engine() { 
    if(stage) { 
     initialize(); 
     } else addEventListener(Event.ADDED_TO_STAGE,initialize); 
    } 

private function initialize(e:Event = null):void { 
    removeEventListener(Event.ADDED_TO_STAGE,initialize); 
    // here goes the code that was in Engine constructor 
    trace(stage); 
     ourShip = new Ship(stage); 
     stage.addChild(ourShip); 
     ourShip.x = stage.stageWidth/2; 
     ourShip.y = stage.stageHeight/2; 
     ourShip.addEventListener("hit", shipHit, false, 0, true); 
     stage.addChild(ourShip); 
     scoreHUD = new ScoreHUD(stage); 
     stage.addChild(scoreHUD); 

     for (var i:int = 0; i < numCloud; i++) 
     { 
      stage.addChildAt(new Cloud(stage), stage.getChildIndex(ourShip)); 

     } 
     for (var b:int = 0; b < numStar; b++) 
     { 
      stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip)); 
     } 

     addEventListener(Event.ENTER_FRAME, loop, false, 0, true); 
} 

的主要问题是,你的Engine类是写在假定,即stage已经接近,但是当你加载的SWF,其stage为空,而SWF不会添加到舞台上。为了规避这种情况,通常的做法是使用Event.ADDED_TO_STAGE侦听器,并开始将代码放在那里,而不是构造函数。

+0

这工作谢谢你分配你的时间!你真棒 – 2013-04-26 08:44:22

+0

要标记问题已解决,请点击最有帮助的答案下方的“打勾”。这有助于将StackOverflow作为一个社区改进,并增加了某人回答您未来问题的机会。 – Vesper 2013-04-26 10:12:42

相关问题