My code works except for one flaw it will not lock to the final enemy. In the level you are tasked with protecting the village and keeping the peasants alive. I am unable to kill the final enemy in time every time I submit It ignores the enemy. Can you please find my error ?
// This function attacks the nearest enemy.
function findAndAttackEnemy() {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
// Define a function to cleave enemies (but only when the ability is ready).
function findAndCleaveEnemy() {
// Find the nearest enemy:
var enemy = hero.findNearestEnemy();
// If an enemy exists:
if (enemy) {
// And if “cleave” is ready:
hero.isReady;
// It’s time to cleave!
hero.cleave(enemy);
}
}
// In your main loop, patrol, cleave, and attack.
while (true) {
// Move to the patrol point, cleave, and attack.
hero.moveXY(35, 34);
findAndCleaveEnemy();
findAndAttackEnemy();
// Move to the other point:
hero.moveXY(60, 32);
// Use findAndCleaveEnemy function:
hero.findAndCleaveenemy;
// Use findAndAttackEnemy function:
hero.findAndAttackenemy;
// This function attacks the nearest enemy.
function findAndAttackEnemy () {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
// Define a function to cleave enemies (but only when the ability is ready).
function findAndCleaveEnemy () {
// Find the nearest enemy:
var enemy = hero.findNearestEnemy();
// If an enemy exists:
if (enemy) {
// And if “cleave” is ready:
hero.isReady;
// It’s time to cleave!
hero.cleave(enemy);
}
}
// In your main loop, patrol, cleave, and attack.
while (true) {
// Move to the patrol point, cleave, and attack.
hero.moveXY(35, 34);
findAndCleaveEnemy();
findAndAttackEnemy();
// Move to the other point:
hero.moveXY(60, 32);
// Use findAndCleaveEnemy function:
hero.findAndCleaveenemy;
// Use findAndAttackEnemy function:
hero.findAndAttackenemy;
}
I’m going to be the JavaScript console: hero.isReady is not used anywhere, did you mean to call it as a function? hero.findAndCleaveenemy is not used anywhere, did you mean to call it as a function? hero.findAndAttackenemy is not used anywhere, did you mean to call it as a function?
This should be done differently, you have to implement the hero.isReady in an if-statement, and this is how it should look like: hero.isReady("cleave"); it needs an argument, in this case it’s cleave
Hint:
if (hero.isReady("cleave")) {
hero.cleave(enemy)
}
As for this, it is not a built-in method for the hero to use. You should be calling it this way:
// Use findAndCleaveEnemy function:
findAndCleaveEnemy;
// Use findAndAttackEnemy function:
findAndAttackEnemy()