2011-02-22 67 views
0

我遇到錯誤#1009:無法訪問空對象引用的屬性或方法。我並不確定如何自己解決這個問題,我追蹤瞭解對象var enemySpawnTimer:Timer是否實際爲空,而不是。所以我不明白爲什麼我會得到這個錯誤。AS3在計時器變量上獲得錯誤#1009

無論如何,這裏是我的代碼,它是我用來產生從屏幕頂部到底部的塊的類,並且一旦到達屏幕底部就從舞臺上移除。

package scripts 
{ 
    import flash.events.Event; 
    import flash.display.Stage; 
    import flash.events.Event; 
    import flash.events.TimerEvent; 
    import flash.utils.Timer; 
    import flash.display.DisplayObject; 

    public class EnemySpawner 
    { 
     var stageRef:Stage; 
     var target:Player; 

     //vector variables 
     public static var vectorBlock:Vector.<Block> = new Vector.<Block>(); 

     //enemy variables 
     public static var block:Block; 

     //timer variables 
     var enemySpawnTimer:Timer = new Timer(250); 

     //score variables 
     public static var pointsBlock:Number = 0; 

     public function EnemySpawner(stageRef:Stage, target:Player) 
     { 
      this.stageRef = stageRef; 
      this.target = target; 

      enemySpawnTimer.addEventListener(TimerEvent.TIMER, SpawnBlocks, false, 0, true); 
      enemySpawnTimer.start(); 
     } 

     private function SpawnBlocks(e:TimerEvent):void 
     { 
      block = new Block(stageRef); 
      pointsBlock = block.pointsGiven; 

      vectorBlock.push(block); 
      stageRef.addChild(block); 

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

     private function Update(e:Event):void 
     { 
      //remove enemies when they pass the bottom of the stage 
      for each(var block:Block in vectorBlock) 
      { 
       if(block.y > Bounds.rectH + block.height) 
       { 
        RemoveFromList(block); 
       } 
      } 

      target.addEventListener("playerDeath", StopSpawning, false, 0, true); 
     } 

     private function RemoveFromList(block:Block):void 
     { 
      vectorBlock.splice(vectorBlock.indexOf(block), 1); 

      if(stageRef.contains(block)) 
      { 
       stageRef.removeChild(block); 
      } 
     } 

     private function StopSpawning(e:Event):void 
     { 
      enemySpawnTimer.stop(); 
     } 
    } 
} 

這是我得到的錯誤,不幸的是,在Flash中使用腳本編輯器並不太有助於導致錯誤的位置或內容。

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at scripts::EnemySpawner/SpawnBlocks() 
at flash.utils::Timer/_timerDispatch() 
at flash.utils::Timer/tick() 
+0

它可能不是定時器導致您的空指針異常。錯誤信息是否有其他說明,你有行號還是其他的? – weltraumpirat 2011-02-22 20:20:13

+0

我加了錯誤。不幸的是我沒有得到行號,除非它在編譯時出錯。我曾經想過在所有地方放置「跟蹤」語句來找出可能爲空的東西,但我不知道我在找什麼,所以拋出跟蹤語句可能毫無意義。 – RamenNoodles 2011-02-22 20:58:20

回答

0

如果我正確理解錯誤的最後一點,是不是必須是空的SpawnBlocks()方法內的東西?我會看stageRef和vectorBlock,也許block.pointsGiven。

1

這個錯誤時,東西爲空,通常會出現,我最好的猜測是「stageRef」 嘗試之前

this.stageRef = stageRef; 

一個你可以爲更好的調試做更多的事情將

stageRef = new Stage(); 

是允許它,請點擊文件/發佈設置/閃存並勾選「許可證調試「,然後再次重新發布以查看您的錯誤,這次它會讓您知道導致錯誤的幀和行號。