0

尝试我的代码时出现此错误:

ArgumentError:错误 #2025:提供的 DisplayObject 必须是调用者的子对象。在 Flash.display::DisplayObjectContainer/removeChild() 在 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 回答