2013-04-26 73 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