2013-03-16 48 views
0

當我想刪除小孩我得到錯誤提供的DisplayObject必須是調用者的孩子。我嘗試了很多不同的方法,我在這裏找到了stackoverflow,但都沒有工作。刪除小孩拋出錯誤

if(player.hitTestObject(activePowerup as DisplayObject) 
{ 
    activePowerup.onPlayerPick(player); 
    this.parent.removeChild(activePowerup as DisplayObject); 
    activePowerups.splice(activePowerup, 1); 
} 

我不知道這個問題是什麼,但我希望你們能幫助我。

+0

我認爲你可以在第4行刪除'作爲DisplayObject'。 – David 2013-03-16 19:40:34

+0

'trace(parent.getChildIndex(activePowerup))'print?是的,如果activePowerup繼承自DisplayObject,則不需要在示例中使用'作爲DisplayObject' – duTr 2013-03-17 04:08:16

回答

1

如果您的activePowerup肯定是this.parent的孩子,那麼我懷疑問題是您的拼接。假設activePowerupsArray,splice()不會像您期望的那樣工作(see the docs here)。你需要給splice()指數activePowerup

activePowerups.splice(activePowerups.indexOf(activePowerup), 1); 

否則,你可能拼接出錯誤的通電,然後嘗試運行此代碼的下一次刪除同一個孩子的兩倍。