Safe Distance - cleave not running

Hi everyone!

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

1 Like

Your problem is the same as your Other post

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);
    }
}


(I didn’t see that t still didn’t work, lol)

looks like your problem is still the same.

Those three brackets at the end should be before the else statement. By including the else statement in the brackets, it can’t run the cleave.

I don’t think I agree. “Else” works for "if hero is not ready to “cleave”, then attack. Not: “if there’s no enemy” then attack

Okay then, hope you get past this level.

1 Like