Actionscript 3 As3:_root.removeChild(此)在特定情况下不使用数组?

Actionscript 3 As3:_root.removeChild(此)在特定情况下不使用数组?,actionscript-3,runtime-error,movieclip,addchild,projectile,Actionscript 3,Runtime Error,Movieclip,Addchild,Projectile,我有一门忍者之星的课程 在循环函数中,我有: private function loop (event:Event):void { trace(this); for (var i:int=0; i<_root.guardArray.length; i++) { //if movieClip at position [i] (enemy) hits this if

我有一门忍者之星的课程

在循环函数中,我有:

    private function loop (event:Event):void
    {           
    trace(this);

        for (var i:int=0; i<_root.guardArray.length; i++) 
        {
            //if movieClip at position [i] (enemy) hits this
            if (_root.guardArray[i].hitTestObject(this)) 
            {
                if(this.hitTestObject(_root.guardArray[i].hitbox))
                {
                    _root.guardArray[i].killThis();
                    _root.removeChild(this);
                    removeEventListener(Event.ENTER_FRAME, loop);
                }
            }
        }

            y-=Math.cos(rotation/-180*Math.PI)*(ninjaStarSpeed);
            x-=Math.sin(rotation/-180*Math.PI)*(ninjaStarSpeed);


        if(this.x < 0 || this.x > _root.stagewidth || this.y > _root.stageheight || this.y < 0)
        {
            _root.removeChild(this);
            removeEventListener(Event.ENTER_FRAME, loop);
        }
    }
}
私有函数循环(事件:事件):无效
{           
跟踪(这个);
对于(变量i:int=0;i_root.stagewidth | | | this.y>_root.stageheight | | this.y<0)
{
_根。removeChild(本);
removeEventListener(Event.ENTER_FRAME,循环);
}
}
}
当忍者之星离开屏幕时,它成功地移除了自己,没有任何错误

然而,当它击中一个防护装置时,它会自动移动,但在第40行时会给我一个#2025错误

这是第40行:_root.removeChild(This);-这是数组冲突检查的一部分


是flash被窃听了还是我做错了什么?

是的,你做错了什么,因为出错;)

为您准备的代码片段:

private function safeRemove():void{
    if(this.parent != null){
        this.parent.removeChild(this);
    }
}
将此方法添加到NinjaStar类中,并使用它。所以第40行代码看起来像

//And don't forget not only kill guard, but also clear reference on him from the guardArray.
_root.guardArray[i].killThis();
safeRemove();
removeEventListener(Event.ENTER_FRAME, loop);

我太爱你了D