2013-01-21 41 views
1

我只是不知道該怎麼做。我一直在尋找,現在通過這一個小時,我不斷收到錯誤讀數:AS3 - 帶定時器的TypeError#1009

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
    at Shooter_Enemy/shotHandler() 
    at flash.utils::Timer/_timerDispatch() 
    at flash.utils::Timer/tick() 

當我調試它,它指向哪裏「seekingBullet」被添加到舞臺的代碼行。任何幫助解決這個問題都會受到歡迎。

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    import flash.utils.Timer; 

    public class Shooter_Enemy extends MovieClip 
    { 
     private var yMove:int = 2; 
     private var shootTimer:Timer = new Timer(500); 

     public function Shooter_Enemy() 
     { 
      this.name = "mc_shooter_enemy"; 
      this.addEventListener(Event.ENTER_FRAME,enemyMove); 

      shootTimer.start(); 
      shootTimer.addEventListener(TimerEvent.TIMER,shotHandler); 
     } 

     public function addShooterEnemy(X:int):void 
     { 
      this.x = X; 
      this.y = 0; 
     } 
     public function removeEnemy() 
     { 
      shootTimer.removeEventListener(TimerEvent.TIMER,shotHandler); 
      shootTimer.stop(); 

      this.removeEventListener(Event.ENTER_FRAME,enemyMove); 
      this.x = 0; 
      this.y = (stage.height + this.height); 
     } 
     private function shotHandler(te:TimerEvent):void 
     { 
      var seekingBullet:SeekingBullet = new SeekingBullet(); 
      Main.seekingBulletArray.push(seekingBullet); 
      stage.addChild(seekingBullet); 
      seekingBullet.addSeekingBullet(this.x,this.y); 
     } 
     private function enemyMove(e:Event) 
     { 
      this.y += yMove; 
     } 
    } 
}  
+1

什麼主?它在哪裏初始化並正在seekBulletArray創建? –

+0

當你追蹤(階段);'stage.addChild'之前會發生什麼事情 – Daniel

+0

嘗試在Shooter_Enemy實際添加到舞臺後啓動計時器;也就是在Shooter_Enemy構造函數中爲Event.ADDED_TO_STAGE添加一個事件偵聽器 – Luis

回答

0

如果它是實際的,使用階段一個很好的做法是聽* Event.ADDED_TO_STAGE *,然後開始你的活動,如:

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    import flash.utils.Timer; 

    public class Shooter_Enemy extends MovieClip 
    { 
     private var yMove:int = 2; 
     private var shootTimer:Timer = new Timer(500); 

     public function Shooter_Enemy() 
     { 
      this.name = "mc_shooter_enemy"; 
      this.addEventListener(Event.ENTER_FRAME,enemyMove); 

      shootTimer.addEventListener(TimerEvent.TIMER, shotHandler); 
      addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 
     } 

     public function addShooterEnemy(X:int):void 
     { 
      this.x = X; 
      this.y = 0; 
     } 
     public function removeEnemy() 
     { 
      shootTimer.removeEventListener(TimerEvent.TIMER,shotHandler); 
      shootTimer.stop(); 

      this.removeEventListener(Event.ENTER_FRAME,enemyMove); 
      this.x = 0; 
      this.y = (stage.height + this.height); 
     } 
     private function addedToStageHandler(e:Event) 
     { 
      shootTimer.start(); 
     } 
     private function shotHandler(te:TimerEvent):void 
     { 
      var seekingBullet:SeekingBullet = new SeekingBullet(); 
      Main.seekingBulletArray.push(seekingBullet); 
      stage.addChild(seekingBullet); 
      seekingBullet.addSeekingBullet(this.x,this.y); 
     } 
     private function enemyMove(e:Event) 
     { 
      this.y += yMove; 
     } 
    } 
}