每当我在Adobe Flash CS4中编译项目时,我都会遇到以下错误消息:
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]);
}
}
任何意见都表示赞赏。谢谢。 :)
答案 0 :(得分: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]);
}
}
答案 1 :(得分: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;
否则,如果您没有再循环遍历该数组,那么您正在将某个子项添加到数组中,该数组不是您要从中删除的显示对象的子项。