I’m a little puzzled as to why “cleave” is not working. The code is running, there are no compiler or runtime errors, but the hero doesn’t seem to spot the enemy let alone kill it. Below the code. Could you help me figure this one out?
// Collect coins to repair the cart while staying close to the peasant!
while (true) {
var item = hero.findNearestItem();
var enemy = hero.findNearestEnemy();
// Change the if-statement to check if the distance from the hero to the coin is less than 15.
if (item && hero.distanceTo(item) < 15) {
hero.moveXY(item.pos.x, item.pos.y); // Move to the red X after collecting the coin.
}
hero.moveXY(36, 49);
if (enemy) {
if (hero.distanceTo(enemy) < 10) {
if (hero.isReady("cleave")) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
}
}
}
I think the problem is your brackets are in weird places, the if statement brackets should be at the top (before the else statement), not the bottom. Hope this helps
Thanks for reply! And sorry it took me so long to get back to you. I was away. I tried changing the brackets. Didn’t help. He still doesn’t want to do “cleave”
Here’s the code:
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.moveXY(enemy.pos.x, enemy.pos.y);
if (hero.distanceTo(enemy) < 10) {
if (hero.isReady("cleave")) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
}
}
var item = hero.findNearestItem();
if (item && hero.distanceTo(item) < 15) {
hero.moveXY(item.pos.x, item.pos.y);
hero.moveXY(36, 49);
}
}