[Solved] Chained statements like .moveXY() and behavior override each other?

My post is marked as solution, but the way I had changed the @AnSeDra code is wrong. It will fail if we have points A(x0,y0), B(x1,y1), C(x2, y2) where x0=x1 and y1=y2 and similar. AnseDra solution is OK, he only needs to replace
while (k < 3) with while(true)
and
archer.pos.y = xx to archer.pos.y == xx
I’ll redeem my sins by showing how to move the hero and the minions by his method :slight_smile:


and the code:

var k = 0,
    x = hero.pos.x,
    y = hero.pos.y;
var peasant = hero.findByType("peasant")[0],
    l = 0,
    xp = peasant.pos.x,
    yp = peasant.pos.y;
while (true){
    if (k === 0){
        hero.move(Vector(x-20,y));
        if ( hero.pos.x == x-20 && hero.pos.y == y)  k=1;
    }
    else if ( k == 1){
        hero.move(Vector(x-20,y-20));
        if ( hero.pos.x == x-20 && hero.pos.y == y-20)  k=2;
    }
    else if ( k == 2){
        hero.move(Vector(x , y-20));
        if ( hero.pos.x == x && hero.pos.y == y-20) k=3;
    }
    else if ( k == 3){
        hero.move(Vector(x, y));
        if ( hero.pos.x == x && hero.pos.y == y) k=0;
    }    
/////////////////////////////////////////////////////////////////////////    
    if (l === 0){
        hero.command(peasant, "move",Vector(xp-20,yp));
        if ( peasant.pos.x == xp-20 && peasant.pos.y == yp)  l=1;
    }
    else if ( l == 1){
        hero.command(peasant, "move",Vector(xp-20,yp-20));
        if ( peasant.pos.x == xp-20 && peasant.pos.y == yp-20)  l=2;
    }
    else if ( l == 2){
        hero.command(peasant, "move",Vector(xp , yp-20));
        if ( peasant.pos.x == xp && peasant.pos.y == yp-20) l=3;
    }
    else if ( l == 3){
        hero.command(peasant, "move",Vector(xp, yp));
        if ( peasant.pos.x == xp && peasant.pos.y == yp) l=0;
    }    
}

3 Likes