Actionscript 我想在测试孩子时删除他们,但索引似乎已关闭

Actionscript 我想在测试孩子时删除他们,但索引似乎已关闭,actionscript,Actionscript,好的,当我的激光击中敌人时,我想移除激光和敌人,但是 我的循环不知怎的在索引外搜索并使程序崩溃。我做错了什么 package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.geom.Rectangle; import flas

好的,当我的激光击中敌人时,我想移除激光和敌人,但是 我的循环不知怎的在索引外搜索并使程序崩溃。我做错了什么

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.geom.Rectangle;
    import flash.ui.Mouse;
    import flash.utils.Timer;

    [SWF(width = 500, height = 500, frameRate = 25, backgroundColor = 0x000000)]

    public class Main extends Sprite 
    {
        private var craft:Craft = new Craft();
        private var laser:Laser;
        private var laserCollection:Vector.<Laser> = new Vector.<Laser>;
        private var laserSpeed:Number = 5;

        private var newEnemyTimer:Timer = new Timer(2000);
        private var enemy:Enemy;
        private var enemyCollection:Vector.<Enemy> = new Vector.<Enemy>;
        private var enemySpeed:Number = 5;

        public function Main()
        {
            addChild(craft);
            //Create a bounding box for the draggable area.
            var rect:Rectangle = new Rectangle(12, 460, 476, 0);
            //If you set startDrags first parameter, LockCenter to true, you don't have to explicitly set the targets position to the mouses position.
            craft.startDrag(true, rect);
            Mouse.hide();

            newEnemyTimer.start();
            newEnemyTimer.addEventListener(TimerEvent.TIMER, newEnemy);

            stage.addEventListener(MouseEvent.CLICK, shoot);

            addEventListener(Event.ENTER_FRAME, update);
        }   

        private function update(e:Event):void
        {
            updateLasers();
            updateEnemies();
            checkForHit();

            trace("lasers:" + laserCollection.length + ", enemies:" + enemyCollection.length);
        }

        private function checkForHit():void
        {
            if (laserCollection.length > 0)
            {
                for (var i:int = 0; i < laserCollection.length; i++) 
                {
                    for (var j:int = 0; j < enemyCollection.length; j++) 
                    {
                        if (laserCollection[i].hitTestObject(enemyCollection[j]))
                        {
                            removeChild(laserCollection[i]);
                            laserCollection.splice(i, 1);

                            removeChild(enemyCollection[j]);
                            enemyCollection.splice(j, 1);

                            //The program crashes here because the index is outside the reach,
                            //i.e if I have 8 laser beams out and only 3 enemies, it tries to remove enemy[8]
                            // I THINK???
                        }
                    }
                }
            }
        }

        private function updateLasers():void
        {
            if (laserCollection.length > 0)
            {
                for (var i:int = 0; i < laserCollection.length; i++) 
                {
                    if (laserCollection[i].y > 10)
                    {
                        laserCollection[i].y -= laserSpeed;
                        laserCollection[i].alpha -= 0.01;
                    }
                    else
                    {
                        //Can the problem be that I also remove the laser here?
                        //It seem to crash even if I hit an enemy at the bottom of the stage so shouldn't be.
                     removeChild(laserCollection[i]);
                     laserCollection.splice(i, 1);
                    }
                }
            }
        }

        private function updateEnemies():void
        {
            if (enemyCollection.length > 0)
            {
                for (var i:int = 0; i < enemyCollection.length; i++) 
                {
                    if (enemyCollection[i].y < 500)
                    {
                        enemyCollection[i].y += enemyCollection[i].getSpeed();
                    }
                    else
                    {
                        enemyCollection[i].y = -20;
                        enemyCollection[i].setSpeed();
                        enemyCollection[i].x = Math.random() * stage.stageWidth;
                    }
                }
            }
        }

        private function newEnemy(e:TimerEvent):void
        {
            enemy = new Enemy();
            addChild(enemy);
            enemy.x = Math.random() * stage.stageWidth;
            enemy.y = -30;
            enemyCollection.push(enemy);
        }

        private function shoot(e:MouseEvent):void
        {
            laser = new Laser();
            addChild(laser);
            laserCollection.push(laser);
            laser.x = craft.x;
            laser.y = craft.y;
        }
    }   
}

在迭代过程中删除数组的元素会导致一个问题:在删除数组[i]中的元素时,以下元素将向上移动。在删除一个元素之后,在同一个周期中,数组[i]中仍有一个元素。当下一个循环周期开始时,i递增,因此您跳过了一个元素

两种可能的解决办法:

如果移除了一个元素,则将计数器i和j都减小。 向后循环,因此尚未检查的元素的索引不会更改。
另一件事:这里不需要像laserCollection.length>0这样的检查,因为如果集合为空,循环将不会运行。

如果他总是删除索引0处的元素,会怎么样?@因为如果他删除数组[0]并递减i,则循环将为-1。当循环继续时,它将再次增加到0,因此它始终在正确的范围内。只要不在递减后立即使用索引,就可以了。