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);
}
}
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…
Despite the tips that everyone has mentioned, my hero still doesn’t move. Can someone help me out?
// Use while loops to pick out the ogre
while(true) {
var enemies = hero.findEnemies();
var enemyIndex = 0;
// Wrap this logic in a while loop to attack all enemies.
// Find the array's length with: enemies.length
while (enemies.length) {
var enemy = enemies[enemyIndex];
// "!=" means "not equal to."
if (enemy.type != "sand-yak") {
// While the enemy's health is greater than 0, attack it!
while (enemy.health > 0) {
hero.attack(enemy);
}
}
}
// Between waves, move back to the center.
hero.moveXY(40, 32);
}
After I did that the duck shows up again and says "Line 14: TypeError: Cannot read property ‘type’ undefined.
// Use while loops to pick out the ogre
while(true) {
var enemies = hero.findEnemies();
var enemyIndex = 0;
// Wrap this logic in a while loop to attack all enemies.
// Find the array's length with: enemies.length
while (enemies.length) {
var enemy = enemies[enemyIndex];
// "!=" means "not equal to."
if (enemy.type != "sand-yak") {
// While the enemy's health is greater than 0, attack it!
while (enemy.health > 0) {
hero.attack(enemy);
}
}
enemyIndex += 1;
}
// Between waves, move back to the center.
hero.moveXY(40, 32);
}