2012-07-30 59 views
1

我在AS3中做了一個遊戲,我有2個文件。一個是HWMain和HWGame。當我點擊開始按鈕腳本從HWMain切換到HWGame,但我得到了這個錯誤。AS3:錯誤MethodInfo-26和25

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at HWGame() 
at MethodInfo-26() 
at MethodInfo-25() 

這是我的代碼。

public class HWGame extends MovieClip 
{ 
    var INIT_STATE:String = "INIT_STATE"; 
    var READY_STATE:String = "READY_STATE"; 
    var PLAYER_STATE:String = "PLAYER_STATE"; 
    var PLAY_STATE:String = "PLAY_STATE"; 
    var END_STATE:String = "END_STATE"; 
    var gameState:String; 

    //And another variable 

    public function HWGame() 
    { 
     gameState = INIT_STATE; 
     trace(gameState); 
     stage.addEventListener(Event.ENTER_FRAME, gameLoop); 

     function gameLoop(e:Event):void 
     { 
      switch (gameState) 
      { 
       case INIT_STATE : 
        initGame(); 
        break; 
       case READY_STATE : 
        ready(); 
        break; 
       case PLAYER_STATE : 
        startPlayer(); 
        break; 
       case PLAY_STATE : 
        playGame(); 
        break; 
       case END_STATE : 
        endGame(); 
        break; 
      } 
     } 

     function initGame():void 
     { 
      //I write the long code 
     } 

     function ready():void 
     { 
      //I write the long code 
     } 

     function startPlayer():void 
     { 
      //I write the long code 
     } 

     function playGame():void 
     { 
      //I write the long code 
     } 

     function endGame():void 
     { 
      //I write the long code 
     } 
    } 
} 

我試圖修復它,我認爲錯誤是在gameState = INIT_STATE。 我該怎麼辦?

謝謝。

+0

它更可能是這個'MovieClip'還未添加到舞臺。因此'stage'可能在'stage.addEventListener(Event.ENTER_FRAME,gameLoop);'處爲null。 – 2012-07-30 04:54:56

+0

@JasonSturges,所以..我必須刪除階段代碼? – Faras 2012-07-30 06:28:29

回答

0

從您所引用的內容來看,很可能您的HWGame實例尚未添加到顯示列表中;因此,stagenull當你撥打:

stage.addEventListener(Event.ENTER_FRAME, gameLoop); 

訪問stage之前,你可以等到添加到舞臺:

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 

    public class HWGame extends MovieClip 
    { 

     var INIT_STATE:String = "INIT_STATE"; 
     var READY_STATE:String = "READY_STATE"; 
     var PLAYER_STATE:String = "PLAYER_STATE"; 
     var PLAY_STATE:String = "PLAY_STATE"; 
     var END_STATE:String = "END_STATE"; 
     var gameState:String; 

     public function HWGame() 
     { 
      gameState = INIT_STATE; 

      addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 
     } 

     protected function addedToStageHandler(event:Event):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 

      stage.addEventListener(Event.ENTER_FRAME, gameLoop); 
     } 

     protected function gameLoop(e:Event):void 
     { 
      switch (gameState) 
      { 
       case INIT_STATE: 
        initGame(); 
        break; 
       case READY_STATE: 
        ready(); 
        break; 
       case PLAYER_STATE: 
        startPlayer(); 
        break; 
       case PLAY_STATE: 
        playGame(); 
        break; 
       case END_STATE: 
        endGame(); 
        break; 
      } 
     } 

     protected function initGame():void 
     { 
      //I write the long code 
     } 

     protected function ready():void 
     { 
      //I write the long code 
     } 

     protected function startPlayer():void 
     { 
      //I write the long code 
     } 

     protected function playGame():void 
     { 
      //I write the long code 
     } 

     protected function endGame():void 
     { 
      //I write the long code 
     } 
    } 
} 
+0

非常感謝你.. 現在我知道它是如何工作..:D – Faras 2012-07-30 07:10:19