Error on AS3: TypeError: Error #1010: A term is undefined and has no properties

谁说我不能喝 提交于 2019-12-13 00:43:50

问题


I'm trying to create a collision code involving an array of bullets and an array of zombies. But when it tried this code:

    for(var bu:int = 0;bu < bullets.length; bu++){
        for(var zo:int = 0;zo < zombieCount.length; zo++){
            if(bullets[bu].hitTestObject(zombieCount[zo])){
                stage.removeChild(zombieCount[zo]);
                zombieCount.splice(zo, 1);
                stage.removeChild(bullets[bu]);
                bullets.splice(bu, 1);

                trace("hot hit")
            }
        }
    }

I sometimes get an error message. I tried this code:

    for(var bu:int = 0;bu < bullets.length; bu++){
        for(var zo:int = 0;zo < zombieCount.length; zo++){
            if(bullets[bu].hitTestObject(zombieCount[zo])){
                stage.removeChild(zombieCount[zo]);
                if(zombieCount.splice(zo,1)!=null){
                    zombieCount.splice(zo, 1)
                }
                stage.removeChild(bullets[bu]);
                bullets.splice(bu, 1)
                if(bullets.splice(bu,1)!=null){
                    bullets.splice(bu, 1)
                }               
                trace("hot hit")
            }
        }
    }

However, even though the message doesn't appear, the objects(or rather its remains?) just stops there. If i go back to my original code, I'll keep receiving error messages. Please help.


回答1:


Most likely the issue is because of two things:

  1. You're splicing your array inside a loop that iterates over said array forwards.

    If you're going to do this, you should iterate backwards so it doesn't mess up the index.

For example, let's say zombieCount has 3 elements. The first iteration zo = 0, let's say your hit test succeeds, you splice the array, now zombieCount has 2 elements. The next iteration, zo=1, but the item that used to be referenced by zombieCount[1] is actually in zombieCount[0] now. So you've ended up skipping an item.

  1. You remove the bullet, but don't break out of the inner loop (that loops through all the zombies) - the problem here is if more than one zombie is touching the bullet, you'll end up trying to remove the bullet multiple times and un-intentionally splicing different bullets out of the array. The error is probably because at some point your index bu becomes out of range as a result of this issue.

For exmaple, let's say bullets array has 2 elements and your zombie array has 3 elements. The first iteration bu is 0, let's say the hittest succeeds on the first zombie in the array. So you splice bullets, which now has 1 element. Let's say the second zombie in the array also passes the hittest. Now your splicing bullets again, except it's the second item that ends up getting spliced. Let's say the third zombie in the array passes the hitest too, now there is nothing left in the bullets array, but you trying to splice it anyway and remove the non-existent object from the stage.

Try this instead:

//iterate backwards, so if you remove an item from the array, it's always the last item and won't throw off order of the array
for(var bu:int = bullets.length-1;bu >= 0; bu--){
    for(var zo:int = zombieCount.length-1;zo >= 0; zo--){
        if (bullets[bu].hitTestObject(zombieCount[zo])) {
            //remove the zombie
            stage.removeChild(zombieCount[zo]);
            zombieCount.splice(zo, 1);

            //remove the bullet
            stage.removeChild(bullets[bu]);
            bullets.splice(bu, 1);

            trace("hot hit");

            break; //break out of this inner loop (through all zombies), since the bullet has been destroyed
        }
    }
}


来源:https://stackoverflow.com/questions/30632465/error-on-as3-typeerror-error-1010-a-term-is-undefined-and-has-no-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!