本文介绍了删除子项和 AS3 2025 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图从数组中删除一个项目符号,但似乎无法摆脱这个错误.

Trying to remove a bullet from an array and cannot seem to get rid of this error.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at Template/gameLoop()

如果子弹被击中或超过某个 x 值,我试图将它从数组中移除.

I am trying to remove the bullet from the array if it is hit or if it goes past a certain x value.

for (var oo:int=0;oo<bArray2.length;oo++)
    {
        bullet2=bArray2[oo];
        if(bullet2.x<500 && bullet2!=null && bArray2[oo]!=null)
            {
                bullet2.x += 3;
            }
            else
            {
                removeChild(bullet2);
                bArray2.splice(oo, 1);

            }


                if(boss.hitTestObject(bArray2[oo])&& bArray2[oo]!=null && boss!=null)
                {

                    removeChild(bArray2[oo]);
                    bLives-=1;
                    bArray2[oo]=null;
                    bArray2.splice(oo, 1);


                }//end if


    }

推荐答案

看看removeChild() :

从 DisplayObjectContainer 实例的子列表中删除指定的子 DisplayObject 实例.

所以要成功执行该函数,它应该在我们将删除的子项( DisplayObject )的 DisplayObjectContainer 父项上执行,但如果我们在此之外parent,编译器会以当前的DisplayObjectContainer为parent,以这个例子来了解更多:

So to get that function executed successfully, it should be executed on the DisplayObjectContainer parent of the child ( DisplayObject ) which we will remove, but if we are outside this parent, the compiler will take the current DisplayObjectContainer as the parent, take this example to understand more :

trace(this);    // gives : [object MainTimeline]

var parent_1:MovieClip = new MovieClip()
    addChild(parent_1);
    // add the parent_1 to the MainTimeline

var child_1:MovieClip = new MovieClip()
    addChild(child_1);
    // add the child_1 to the MainTimeline

var child_2:MovieClip = new MovieClip()
    parent_1.addChild(child_2);
    // add the child_2 to parent_1 which is a child of the MainTimeline

removeChild(child_1);
// remove child_1 from the MainTimeline : OK

try {
    removeChild(child_2);
    // remove child_2 from the MainTimeline : ERROR
} catch(e:*){
    trace(e);
    // gives : ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
}

这里我们知道 child_2 不是 MainTimeline 的孩子,这就是为什么我们得到错误 #2025,因为提供的 DisplayObject ( child_2 ) 不是调用者( MainTimeline ).

Here we know that child_2 is not a child of the MainTimeline and that's why we got the Error #2025 because the supplied DisplayObject ( child_2 ) is not a child of the caller ( the MainTimeline ).

所以要删除 child_2 MovieClip,我们必须从它的父对象 parent_1 调用 removeChild() :

So to remove the child_2 MovieClip, we have to call removeChild() from it's parent which is parent_1 :

parent_1.removeChild(child_2); // OK

为了简化或者如果我们不知道它的父级,你可以这样写:

And to simplify or if we don't know it's parent, you can write it :

child_2.parent.removeChild(child_2);

希望能帮到你.

这篇关于删除子项和 AS3 2025 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:10