Error on AS3: TypeError: Error #1010: A term is undefined and has no properties
Error on AS3: TypeError: Error #1010: A term is undefined and has no properties
我正在尝试创建一个涉及一组子弹和一组僵尸的碰撞代码。但是当它尝试这段代码时:
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")
}
}
}
我有时会收到一条错误消息。我试过这段代码:
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")
}
}
}
然而,即使消息没有出现,对象(或者更确切地说是它的遗迹?)就停在那里。如果我回到原来的代码,我会不断收到错误消息。请帮忙
这个问题很可能是由两件事造成的:
您正在将您的数组拼接在一个循环中,该循环向前迭代所述数组。
如果你打算这样做,你应该向后迭代,这样它就不会弄乱索引。
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.
- 你取出子弹,但不要跳出内循环(循环遍历所有僵尸)——这里的问题是如果不止一个僵尸碰到子弹,你最终会尝试多次移除子弹并无意中将不同的子弹拼接出阵列。该错误可能是因为在某些时候您的索引
bu
由于此问题而超出范围。
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.
试试这个:
//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
}
}
}
我正在尝试创建一个涉及一组子弹和一组僵尸的碰撞代码。但是当它尝试这段代码时:
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")
}
}
}
我有时会收到一条错误消息。我试过这段代码:
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")
}
}
}
然而,即使消息没有出现,对象(或者更确切地说是它的遗迹?)就停在那里。如果我回到原来的代码,我会不断收到错误消息。请帮忙
这个问题很可能是由两件事造成的:
您正在将您的数组拼接在一个循环中,该循环向前迭代所述数组。
如果你打算这样做,你应该向后迭代,这样它就不会弄乱索引。
For example, let's say
zombieCount
has 3 elements. The first iterationzo = 0
, let's say your hit test succeeds, you splice the array, nowzombieCount
has 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.
- 你取出子弹,但不要跳出内循环(循环遍历所有僵尸)——这里的问题是如果不止一个僵尸碰到子弹,你最终会尝试多次移除子弹并无意中将不同的子弹拼接出阵列。该错误可能是因为在某些时候您的索引
bu
由于此问题而超出范围。
For exmaple, let's say
bullets
array has 2 elements and your zombie array has3
elements. The first iterationbu
is0
, 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 splicingbullets
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 thebullets
array, but you trying to splice it anyway and remove the non-existent object from the stage.
试试这个:
//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
}
}
}