You need to check if the enemy exists before doing any of that stuff.
var enemy = enemies[curEnemy];
if (enemy) {
on a side note, you can accomplish this a bit easier with a regular for loop.
var enemies = this.findEnemies();
if (enemies.length > 0) {
for(var i = 0; i < enemies.length; ++i) {
var enemy = enemies[i];
if (enemy) {
// do something here
}
}
}
Yes. but it may have 1 enemy in it. your code will get to the next index and then the next one could not exist.
Example.
findEnemies has only 1 enemy in it. an array with index[0] only.
using your code. the first round works. but when you increment it to 1 and it enters the loop again. it fails because enemy is undefined and you aren’t checking if (enemy)
[quote=“sotonin, post:2, topic:1965”]
var enemies = this.findEnemies();
if (enemies.length > 0) {
for(var i = 0; i < enemies.length; ++i) {
var enemy = enemies[i];
if (enemy) {
[/quote]You’re right, my bad!