Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3,从列表中删除对象错误_Actionscript 3_Flash_Actionscript - Fatal编程技术网

Actionscript 3 AS3,从列表中删除对象错误

Actionscript 3 AS3,从列表中删除对象错误,actionscript-3,flash,actionscript,Actionscript 3,Flash,Actionscript,我有一个对象列表,我试图遍历该列表并检查是否有碰撞,此时将播放一个运动tween,最后将执行删除对象的函数 stage.addEventListener(Event.ENTER_FRAME, hitTest); function hitTest(e:Event ):void { for each (bullet in bullets) { if (bullet.parent == null) { bullets.splice

我有一个对象列表,我试图遍历该列表并检查是否有碰撞,此时将播放一个运动tween,最后将执行删除对象的函数

stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(e:Event ):void
{
    for each (bullet in bullets)
    {
        if (bullet.parent == null)
        {
            bullets.splice(bullets.indexOf(bullet),1);
        }
        else if (bullet.hitTestObject(shark))
        {
            trace("HIT1");
            bullet.gotoAndPlay(2); //part that's giving me trouble
            bullets.splice(bullets.indexOf(bullet),1);
            trace("HIT");
        }
        else
        {
            for each (enemy in enemies)
            {
                if (enemy !=null && bullet.hitTestObject(enemy))
                {
                    enemies.splice(enemies.indexOf(enemy),1);
                    enemy.remove();
                    enemy = null;
                    bullets.splice(bullets.indexOf(bullet),1);
                    bullet.remove();
                    break;
                }
            }
        }
       }
通过列表测试各种事情

在bullet类的bullet对象的motion tween设置结束时

stop();
this.remove();
public function remove() {
    parent.removeChild(this);
    this.removeEventListener(Event.ENTER_FRAME, moveMe);
}
这是bullet类中的remove函数

stop();
this.remove();
public function remove() {
    parent.removeChild(this);
    this.removeEventListener(Event.ENTER_FRAME, moveMe);
}
特定误差

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/remove()
at Bullet/frame20()
第20帧是运动tween的最后一帧,具有上面的代码^^


感谢您的帮助。

从正在迭代的数组中删除项会更改数组的长度,并可能导致错误。选项是向后迭代数组,或者使用一个本地临时数组,将“命中数”的索引推入其中,然后在主for循环完成后从主数组中删除,然后迭代temps数组

有关讨论和解决方案示例的类似问题,请参见和