[SOLVED] Kithgard Brawl on JAVASCRIPT

I know this has been answered before but I still don’t get it.

this is my code:

var enemy = hero.findNearestEnemy();

while(true) {
    if(enemy) {
        if(hero.isReady("cleave")) {
            hero.cleave(enemy);
        }
            else {
            hero.attack(enemy);
        }
    }
    else {
        hero.moveXY(item.pos.x, item.pos.y);
    }

hero cleaves and then says “but it’s dead…”. Code get stuck there. only cleave doesnt attack

Welcome to the discourse :partying_face:

I suggest you put this statement inside the while-true loop at the very top

thank you very much. still not sure why var needs to be inside the loop

1 Like

You’re welcome, because it has to refresh every time you kill the enemy. Once you kill it, enemy would be none, so if you have it in the while loop, it will always have the nearest enemy, if it is outside, it would find the nearest at the beginning of the level and ignore the rest

makes sense now, thank you!!

1 Like

anytime :slight_smile:
(20chars)

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.