问题
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:
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
zombieCounthas 3 elements. The first iterationzo = 0, let's say your hit test succeeds, you splice the array, nowzombieCounthas 2 elements. The next iteration,zo=1, but the item that used to be referenced byzombieCount[1]is actually inzombieCount[0]now. So you've ended up skipping an item.
- 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
bubecomes out of range as a result of this issue.
For exmaple, let's say
bulletsarray has 2 elements and your zombie array has3elements. The first iterationbuis0, let's say the hittest succeeds on the first zombie in the array. So you splicebullets, which now has 1 element. Let's say the second zombie in the array also passes the hittest. Now your splicingbulletsagain, 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 thebulletsarray, 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