Enemy.type not working?

var enemy = enemies[curEnemy];
        switch(enemy.type)
        {
        case "munchkin":
            this.attack(enemy);
            this.attack(enemy);
            break;
        case "ogre":
            this.moveXY(41,47);
            break;
        default:
            redo = true;
            curEnemy++;
        }

What am I doing wrong here? http://prntscr.com/5iak90

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

But I am checking for findEnemies(). Does it always return an array?

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!