2010-07-27 56 views
0

我遇到以下錯誤消息時,我編我在Adobe的Flash CS4項目:的ActionScript 3.0的參數錯誤2025(刪除子問題)

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. 
    at flash.display::DisplayObjectContainer/removeChild() 
    at stageRotation/spawnParticle() 
    at flash.utils::Timer/_timerDispatch() 
    at flash.utils::Timer/tick() 

生成錯誤的代碼如下所示:

for (var i:int = 0; i < particleArrayForward.length; i++) { 
    if (particleArrayForward[i] != null) { 
     trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]); 
     this.removeChild(particleArrayForward[i]); 
    } 
} 

任何輸入讚賞。謝謝。 :)

回答

2

removeChild當傳遞的參數不是調用方法的父項的子項時拋出此錯誤。 this對象中的粒子是否被添加爲另一個子容器?

確保它確實是調用者的子:

for (var i:int = 0; i < particleArrayForward.length; i++) { 
    if (particleArrayForward[i] != null && particleArrayForward[i].parent == this) { 
     trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]); 
     this.removeChild(particleArrayForward[i]); 
    } 
} 

如果顆粒不this對象的直接孩子,你可以用其刪除:

for (var i:int = 0; i < particleArrayForward.length; i++) { 
    if (particleArrayForward[i] != null && particleArrayForward[i].parent != null) { 
     trace("particle at " + i + " " + particleArrayForward[i]); 
     trace("parent is " + particleArrayForward[i].parent); 
     particleArrayForward[i].parent.removeChild(particleArrayForward[i]); 
    } 
} 
+0

感謝。有效! – Smiley 2010-07-27 07:22:16

1

您正在通過循環顯示陣列中的所有粒子從顯示對象中移除子項。但是我看不到你在哪裏刪除了數組本身的引用。因此,如果您再次通過particleArrayFoward循環,您將嘗試刪除已被刪除的顯示對象,我將假設這正在發生?

for (var i:int = 0; i < particleArrayForward.length; i++) { 
    if (particleArrayForward[i] != null) { 
     trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]); 
     this.removeChild(particleArrayForward[i]); 
     particleArrayForward[i]=null;//this will fix it but now the length of array will never shrink 

    } 
} 

所以更好的是:

for (var i:int = 0; i < particleArrayForward.length; i++) { 
    if (particleArrayForward[i] != null) { 
     trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]); 
     this.removeChild(particleArrayForward[i]); 
    } 
} 
particleArrayForward = new Array(); 
//or particleArrayForward.length = 0; 

否則,如果你不通過數組循環再然後的地方要添加一個孩子,是不是顯示對象的子陣列的你正試圖從中刪除。