删除子项并将其从数组中删除时键入Error

时间:2013-08-20 10:45:39

标签: actionscript-3

当子弹击中斯巴达时,我在以下函数中遇到类型错误(术语未定义且没有属性)

  

项目符号是一个数组

     

斯巴达是一个数组

并且此功能基本上会移动子弹和斯巴达人,同时检查它们是否发生碰撞,以及它们是否都被移除。

function loop(event:Event)
{
    for (var bcount=0; bcount < bullets.length; bcount++)
    {
        if (bullets[bcount].x <= 1055)
        {
            bullets[bcount].x = bullets[bcount].x + bulletSpeed;
        }
        else
        {
            removeChild(bullets[bcount])
            bullets.splice(bcount, 1)
            bcount--
        }

        for (var spcount=0; spcount<spartans.length; spcount++)
        {
            spartans[spcount].x = spartans[spcount].x - spartanSpeed
            if (bullets[bcount].hitTestObject(spartans[spcount]))
            {
                removeChild(spartans[bcount])
                spartans.splice(spcount, 1)
                spcount--
                removeChild(bullets[bcount])
                bullets.splice(bcount, 1)
                bcount--

            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

首先

在第二个循环中我想你想删除spartans [spcount]

所以改变这一行

 removeChild(spartans[bcount])

  removeChild(spartans[spcount])

第二

在第二个循环中,您应该检查bcount是否小于零。因为在第一个循环和第二个循环中,你们都减少了bcount,所以bcount可以是-1。

例如,项目符号[0]的x大于1055,因此bcount为-1​​。如果没有发生,在第二个循环中,如果碰撞发生在某些时间,bcount将小于0。