Sarven Sheperds let's me attack the yak not the scouts


#1

I can get to defeat the ogres not using the arrays. Since I’m a beginner I would love to learn how to use the arrays. I know it works fine if I were to kill the yaks by changing != to ==. How come it doesn’t attack the rest of the enemy? Thanks.

// Use while loops to pick out the ogre

loop {
var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];
while (enemyIndex < enemies.length) { // Find the array’s length with: enemies.length
if (enemy && enemy.type != “sand-yak”) {
while (enemy.health > 0) {
if (this.isReady(“bash”)) {
this.bash(enemy); }
else if (this.isReady(“cleave”)) {
this.cleave(enemy);}
else {
this.attack(enemy); }
}
// While the enemy’s health is greater than 0, attack it!
}
enemyIndex = 1 + enemyIndex;
}
// Between waves, move back to the center.
if (enemy === 0) {
this.moveXY(40, 32);
}
}


#2

Ok, now I can attack because I changed the index from 0 to 1. But after killing the first two scouts; since the rest take long to appear, I no longer attack anybody…


#3

I’ve fixed something else:
I’ve changed === to !enemy and it goes back to the central X
I still have the problem of not attacking again… Please, help


#4

Hello, tajjeb, and welcome. Please format your code according to the FAQ.

Move the part when you define enemy into the while-loop. The while-loop should look like this:

var enemyIndex = 0;
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy && enemy.type !== "sand-yak") {
        // Do stuff here. 

#5

Sorry I have to read carefully the FAQ.
Thanks for the help. I fixed the code and it worked after adding code to flee from the yaks.
Great