0

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

spartans 是一个数组

子弹是一个数组

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)
            if (bullets.length != 1)
            {
                bcount--;
            }
            
        }
        
    }
        for (var spcount=0; spcount<spartans.length; spcount++)
        {
            spartans[spcount].x = spartans[spcount].x - spartanSpeed;
            if (bullets.length != 0)
            {
                if (bullets[bcount].hitTestObject(spartans[spcount]))
                {
                    removeChild(spartans[spcount])
                    spartans.splice(spcount, 1)
                    removeChild(bullets[bcount])
                    bullets.splice(bcount, 1)
                }
            }
        }


}
4

2 回答 2

1

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

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
        }
    }
}

还修复了另一个问题 - 您需要移动斯巴达人和子弹,并且您需要一个双循环来检查子弹与斯巴达人,所以虽然可以移动子弹然后检查所有斯巴达人,但也必须移动所有斯巴达人一次. 所以移动的斯巴达人被放置在一个单独的循环中。

于 2013-08-21T08:27:40.930 回答
0

通常,for 变量步只能由 for 指令更新。

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

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

最后,bCount 变量漏掉了类型,把 var bCount:int 放进去,spcount 变量也是一样。

评论后编辑变量定义:

var bCount:int = 0 instead var bCount = 0
var spcount:int = 0 nstead var spcount = 0

循环(反向循环):

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
        //}

    }

}

拼接功能 peraphs 对您不利。看这里

于 2013-08-21T07:35:16.130 回答