我試圖讓我的遊戲正常工作時,在我的代碼中不斷收到上面的錯誤消息。代碼如下,其中錯誤似乎正在發生,但我無法找到它;它帶來的錯誤是無法訪問空對象引用的屬性或方法。在blast_game/moveObjects()
無法訪問空對象引用的屬性或方法。在blast_game/moveObjects()
我的代碼:
public function moveObjects(e:Event)
{
for (var i:int=objects.length-1; i>=0; i--)
{
//code to increase speed of objects over time.
objects[i].y += speed;
if (objects[i].y > 400)
{
removeChild(objects[i]);
objects.splice(i,1);
}
//hit test for catcher - if any object hits the catcher
//increase or decrease score respectively.
if (objects[i].hitTestObject(catcher))
{
//increase score when catcher catches "Good" object
if (objects[i].typestr == "good")
{
score += 10;
}
else
//decrease score if catcher catches "Bad" object.
{
score -= 5;
}
//prevents score from going below 0
if (score < 0)
{
score = 0;
}
scoreDisplay.text = "Score: " + score;
//once object hits catcher, remove object from stage.
removeChild(objects[i]);
objects.splice(i,1);
}
//if statement to move screen to promotion screen
//once score reaches past 100
if(score >= 100) {
var promotionScreen:promotion = new promotion();
//stage.removeChild(this)
addChild(promotionScreen);
removeEventListener(Event.ENTER_FRAME, moveObjects);
promotionScreen = stage.stageWidth/2;
promotionScreen = stage.stageHeight /2;
}
//end of if statement
}
//sets catcher to work with mouse movement.
catcher.x = mouseX;
}
}
}
惱人的事情是,它只顯示錯誤偶然,但即使它沒有,它仍然沒有彈出屏幕,它應該由於上面的代碼。
至少,錯誤發生的行號會很有用。確保在CS6的「發佈設置」中啓用了「允許調試」。如果你沒有充分利用你所擁有的工具,你只會讓自己變得更加困難。 – prototypical 2013-04-22 19:21:10