Help Sarven-sheperd


#1

I my code doesn’t seem to work (JS):

loop {
var enemies = this.findEnemies();
var enemy = this.findNearest(enemies);
// Wrap this logic in a while loop to attack all enemies.

    while (enemy.health > 0){
        // "!=" means "not equal to."
        if (enemy && (enemy.type != "sand-yak")) {
            // While the enemy's health is greater than 0, attack it!
            this.attack(enemy);
            this.attack(enemy);
            this.attack(enemy);
        }

        // Between waves, move back to the center.
    }
    this.moveXY(33, 40);
}

it seems to get stuck on "While (enemy.health>0)"
Please help.


#2

Hello, Joe, and welcome.

What the comments mean is to attack all enemies, but I just see you are attacking one, the nearest. Also, you use enemy before you check for an enemy, which will throw an error if there isn’t one. Try creating a variable enemyIndex and use it to run through all enemies.


#3

@Joe_Wee The problem here is that you end up in an endless loop, if the nearest enemy is a sand yak, that does not die on its own :wink: