ActionScript 3.0 参数错误:2025(删除子问题)

发布于 2024-09-11 12:09:19 字数 648 浏览 8 评论 0原文

每当我在 Adob​​e 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]);
    }
}

任何输入都值得赞赏。谢谢。 :)

I am encountering the following error message whenever I compile my project in 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()

the code that generates the error is shown below:

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

Any input appreciated. Thanks. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-09-18 12:09:19

当传递的参数不是调用该方法的父级的子级时,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]);
    }
}

removeChild throws this error when the passed argument is not a child of the parent that called the method. Are particles added as child to another sub-container within this object?

Make sure it is indeed a child of the caller:

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]);
    }
}

If the particles are not direct children of this object, you can remove them using:

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]);
    }
}
琉璃梦幻 2024-09-18 12:09:19

您可以通过循环遍历数组中的所有粒子来从显示对象中删除子项。但是,我看不到您在哪里删除对数组本身中子级的引用。因此,如果您再次循环粒子ArrayFoward,您将尝试删除已删除的显示对象,我假设正在发生这种情况?

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;

否则,如果您不再循环遍历该数组,那么您在数组中的某个位置添加了一个子项,该子项不是您要从中删除的显示对象的子项。

You are removing a child from display object by looping through all the particles in the array. However I can not see where you then remove the reference to the child in the array itself. So if you loop through particleArrayFoward again you will be trying to remove a display object that was already removed which I am going to assume is happening?

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

    }
}

so better yet:

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;

otherwise if you are not looping through that array again then somewhere you are adding a child to the array that is not a child of the display object of which you are trying to remove from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文