非空的錯誤,我有一個代碼,暫停遊戲,和它運行的暫停功能,如下所示:參數孩子必須在AS3
public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
//Check for pause
if(keyboardEvent.keyCode == Keyboard.P)
{
//If the timer is still running
if(gameTimer.running)
{
gameTimer.stop();
Mouse.show();
pauseText = new PauseText();
pauseText.x = 150;
pauseText.y = 100;
addChild(pauseText);
//If the player is using the mouse, resume by clicking on the player
if(mouseControl)
{
player.addEventListener(MouseEvent.CLICK, resumeGame);
pauseText.pauseInformation.text = "click on yourself";
}
else
{
pauseText.pauseInformation.text = "press 'p'";
}
}
else
{
//Only allow the player to resume with P IF he is using the keyboard
//This prevents cheating with the mouse.
if(!mouseControl)
{
gameTimer.start();
removeChild(pauseText);
pauseText = null;
}
}
}
}
遊戲運行完全正常。在我的第一個playthrough中,暫停功能起作用。但是,如果以後我死了,並重新開始遊戲,然後暫停它,我得到以下信息:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Game/onKeyPress()
遊戲依舊運行正常不過。但是,每次我暫停或取消暫停時,都會顯示此錯誤。如果我再次死亡,請重新啓動,然後暫停,出現兩個這些錯誤。從我可以收集它看起來好像它試圖刪除pauseText ...但我已經刪除它在第一個playthrough只是罰款,我用removeChild()然後設置爲null爲我的代碼的其他部分,它的工作原理精細。此外,如果我添加一個跟蹤(「a」);語句緊跟在函數頭後面,在輸出面板上出現「a」之前出現錯誤。
怎麼了?
其他注意事項: 如果我第一次玩遊戲時沒有使用暫停功能,那麼當我在第二次玩遊戲時調用它時沒有錯誤。
啊,解決了這個問題:) gameTimer.running絕對不是假的,因爲gameTimer運行整個遊戲,它的結果在屏幕上可見。但是,爲了達到最佳效果,我決定在暫停功能之前每次進行跟蹤。我在第一次嘗試中得到了真實的陳述,但在第二次嘗試每一個「真實」時,我也得到了「虛假」。然後我意識到這是因爲我忘記刪除eventListener,因此在第二次運行時,以前的監聽器也正在讀取鍵盤輸入並試圖暫停。所以,我刪除了eventListener,現在它已經修復。謝謝! – ChooJeremy