Stuck on "Rich Forager" Level - JavaScript

Hello everyone. Im stuck in this level. When I run it my character just attacks the first oggres’ group and then start moving around without an apparent logic reason and doesn’t even recognize the flags… I’ve been reading again and again the code trying to find an error and I don’t know what more to do. Please somebody help me!!! The code is the next one

while(true) {
var flag = hero.findFlag();
var enemy = hero.findNearestEnemy();
var item = hero.findNearestItem();

if (flag) {
    // What happens when I find a flag?
    hero.pickUpFlag(flag);   
}
else if (enemy) {
    // What happens when I find an enemy?
    var distance = hero.distanceTo(enemy);
    if (distance < 5) {
        if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        }
        else {
            hero.attack(enemy);
        }
    }
}
else if (item) {
    // What happens when I find an item?
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemY, itemX);

}

}

thanksss!

When listing coordinates, the x coordinate always comes first. Since you listed the Y coordinate first [quote=“Dani1905, post:1, topic:7379”]
hero.moveXY(itemY, itemX);
[/quote]

your hero moves to, say (17, 50) instead of (50, 17).

Thanks mate!! such a stupid mistake I diddd!:dizzy_face: thanks a lot!, now it runs perfectly