Actionscript 3 从数组中删除子对象时出现类型错误#1010(对象未定义且没有属性)

Actionscript 3 从数组中删除子对象时出现类型错误#1010(对象未定义且没有属性),actionscript-3,Actionscript 3,当我注释掉函数中的if语句时,这个未定义对象的函数中出现了一个类型错误,错误消失了 斯巴达人是一个阵型 子弹是一个阵列 函数循环(事件:事件) { 对于(变量bcount=0;bcount=0;bcount--) { 如果(子弹[bcount].x你搞乱了子弹循环,你的循环只移动子弹并拼接它们,然后你有一个完全独立的循环来检查vs子弹[bcount],它已经超出了自己的循环,因此bcount==bullets.length因此您在数组外部查询,这会导致1010错误。请将所有用于循环斯巴达人的代码

当我注释掉函数中的if语句时,这个未定义对象的函数中出现了一个类型错误,错误消失了

斯巴达人是一个阵型

子弹是一个阵列

函数循环(事件:事件)
{
对于(变量bcount=0;bcountif(bullets[bcount].x通常,for变量步骤必须仅由for指令更新

您在if语句中递减bCount。因此,在您的情况下,我更喜欢对每个语句使用,并在循环体中管理bCount变量

请注意删除for中的行的顺序

最后,bCount变量丢失了类型,put var bCount:int,spcount变量也是这样

评论后编辑 变量定义:

var bCount:int = 0 instead var bCount = 0
var spcount:int = 0 nstead var spcount = 0
回路(反向回路):

for(变量bcount:int=bullets.length;bcount>=0;bcount--)
{

如果(子弹[bcount].x你搞乱了子弹循环,你的循环只移动子弹并拼接它们,然后你有一个完全独立的循环来检查vs
子弹[bcount]
,它已经超出了自己的循环,因此
bcount==bullets.length
因此您在数组外部查询,这会导致1010错误。请将所有用于循环斯巴达人的代码放在“move bullet”语句旁边的bullets循环中

function loop(event:Event)
{
    for (var spcount=0;spcount<spartans.length; spcount++)
    {
           spartans[spcount].x = spartans[spcount].x - spartanSpeed;
    }
    // first move spartans, they too need to move once 
    // then move bullets, and check vs moved spartans
    for (var bcount=0; bcount < bullets.length; bcount++)
    {
        if (bullets[bcount].x <= 1055)
        {
            bullets[bcount].x = bullets[bcount].x + bulletSpeed;
            // and now, after you moved the bullet, loop spartans vs this bullet
            for (spcount=0; spcount<spartans.length; spcount++)
            {
                if (bullets[bcount].hitTestObject(spartans[spcount]))
                {
                    removeChild(spartans[spcount])
                    spartans.splice(spcount, 1)
                    removeChild(bullets[bcount])
                    bullets.splice(bcount, 1)
                    break; // there's no bullet anymore, stop looping spartans
                }
            }
        }
        else
        {
            removeChild(bullets[bcount])
            bullets.splice(bcount, 1)
            bcount--; // decrease anyway, or really loop backwards
        }
    }
}
函数循环(事件:事件)
{

对于(var spcount=0;spcount)我可以在我的代码中举一个您的建议的例子吗?我不完全确定您的意思,您在调试模式下的哪一点会得到错误?这将有助于了解谢谢!我以前不懂,但现在懂了
for (var bcount:int=bullets.length; bcount >=0; bcount--)
{
    if (bullets[bcount].x <= 1055)
    {
        bullets[bcount].x = bullets[bcount].x + bulletSpeed;
    }
    else
    {
        removeChild(bullets[bcount]);
        // bullets.splice(bcount, 1); - REMOVE IT
        //if (bullets.length != 1)
        //{
        //    bcount--; // DECREMENT FOR VARIABLE, IMHO, IS DEPRECATED
        //}

    }

}
function loop(event:Event)
{
    for (var spcount=0;spcount<spartans.length; spcount++)
    {
           spartans[spcount].x = spartans[spcount].x - spartanSpeed;
    }
    // first move spartans, they too need to move once 
    // then move bullets, and check vs moved spartans
    for (var bcount=0; bcount < bullets.length; bcount++)
    {
        if (bullets[bcount].x <= 1055)
        {
            bullets[bcount].x = bullets[bcount].x + bulletSpeed;
            // and now, after you moved the bullet, loop spartans vs this bullet
            for (spcount=0; spcount<spartans.length; spcount++)
            {
                if (bullets[bcount].hitTestObject(spartans[spcount]))
                {
                    removeChild(spartans[spcount])
                    spartans.splice(spcount, 1)
                    removeChild(bullets[bcount])
                    bullets.splice(bcount, 1)
                    break; // there's no bullet anymore, stop looping spartans
                }
            }
        }
        else
        {
            removeChild(bullets[bcount])
            bullets.splice(bcount, 1)
            bcount--; // decrease anyway, or really loop backwards
        }
    }
}