我遇到錯誤#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()
它可能不是定時器導致您的空指針異常。錯誤信息是否有其他說明,你有行號還是其他的? – weltraumpirat 2011-02-22 20:20:13
我加了錯誤。不幸的是我沒有得到行號,除非它在編譯時出錯。我曾經想過在所有地方放置「跟蹤」語句來找出可能爲空的東西,但我不知道我在找什麼,所以拋出跟蹤語句可能毫無意義。 – RamenNoodles 2011-02-22 20:58:20