0

コードを試すと、次のエラーが表示されます。

ArgumentError: エラー #2025: 指定された DisplayObject は呼び出し元の子でなければなりません。flash.display::DisplayObjectContainer/removeChild() at Main_MouseFollow/onEnterFrame()[C:\Users\Ida\Documents\flash kursen\Space shooter del2\Main_MouseFollow.as:120]

120行目は removeChild(_bullets[j]);

これがコード全体です。Flash を初めて使用するので、このエラーを修正するにはどうすればよいですか?

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main_MouseFollow extends MovieClip
    {
        private var _bullets:Array;
        private var _robotScore:Number;
        private var _playerScore:Number;

        public function Main_MouseFollow()
        {

            //Add event listeners
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            stage.addEventListener("bulletCreated", onBulletCreated);

            _bullets = new Array();
            _robotScore = 0;
            _playerScore = 0;
        }
        private function onAddedToStage(event:Event):void
        {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }
        private function onRemovedFromStage(event:Event):void
        {
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }
        private function onBulletCreated(event:Event)
        {
            _bullets.push(MovieClip(event.target));
        }
        private function onEnterFrame(event:Event):void
        {
            bulletDisplay.text = "Bullets on the stage: " + String(_bullets.length);
            for (var i:int = 0; i < _bullets.length; i++)
            {
                switch (_bullets[i].bulletType)
                {
                    case "circle" :

                        //Check for a collision with the player
                        if (player.hitTestPoint(_bullets[i].x,_bullets[i].y,true))
                        {
                            //Remove the bullet from the stage
                            removeChild(_bullets[i]);

                            //Remove bullet from array
                            _bullets.splice(i,1);

                            //Subtract 1 from the counter to compensate
                            //for the removed element
                            i--;

                            //Update the robot's score
                            _robotScore++;

                            //Update the robot's score display on the stage
                            robotScoreDisplay.text = String(_robotScore);
                        }
                        break;

                    case "star" :

                        //Check for a collision with the robot
                        if (robot.hitTestPoint(_bullets[i].x,_bullets[i].y,true))
                        {
                            //Remove the bullet from the stage
                            removeChild(_bullets[i]);

                            //Remove bullet from array
                            _bullets.splice(i, 1);

                            //Subtract 1 from the counter to compensate
                            //for the removed element
                            i--;

                            //Update the enemy's score
                            _playerScore++;

                            //Update the player's score display on the stage
                            playerScoreDisplay.text = String(_playerScore);
                        }
                        break;
                    }
                }
                //Bullet stage Boundaries:
                for (var j:int = 0; j < _bullets.length; j++)
                {
                    //Top
                    if (_bullets[j].y + _bullets[j].height / 2 < 0)
                    {
                        removeChild(_bullets[j]);
                        _bullets.splice(j, 1);
                        j--;
                    }
                    //Bottom
                    else if (_bullets[j].y - _bullets[j].height / 2 > stage.stageHeight)
                    {
                        removeChild(_bullets[j]);
                        _bullets.splice(j, 1);
                        j--;
                    }
                    //Left
                    else if (_bullets[j].x + _bullets[j].width / 2 < 0)
                    {
                        removeChild(_bullets[j]);
                        _bullets.splice(j, 1);
                        j--;
                    }
                    //Right
                    else if (_bullets[j].x - _bullets[j].width / 2 > stage.stageWidth)
                    {
                        removeChild(_bullets[j]);
                        _bullets.splice(j, 1);
                        j--;
                    }
                }



        }
    }
}
4

1 に答える 1

1

より安全な方法で子供を取り除くようにしてください:

置換 (4 つのケースすべて):

removeChild(_bullets[j]);

と:

if(_bullets[j].parent)
    _bullets[j].parent.removeChild(_bullets[j]);

これにより、エラーが削除されます。ロジックに他に問題がない場合、これで問題全体が修正されます。

于 2013-01-20T18:39:44.467 に答える