0

忍者スターのクラスがあります。

ループ関数には、次のものがあります。

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

忍者の星は、画面の外に出たときにエラーなく正常に削除されます。

ただし、ガードにヒットすると、それ自体は削除されますが、40 行目で #2025 エラーが発生します。

これは 40 行目です: _root.removeChild(this); - 配列の衝突チェックの一部です。

フラッシュがバグっていますか、それとも私が何か非常に間違ったことをしていますか?

4

1 に答える 1

1

うん、エラーのために何か間違ったことをしている;)

コード スニペット:

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);
于 2014-03-04T22:11:38.440 に答える