Basin Stampede - Being run over by yaks when they spawn in hero's y position

I’m trying to use the code below but that code means I get run over by yaks when a yak spawns close to my y position. Whereas the 2nd set of codes are able to run just fine. The only difference between the 2 codes is " if(enemy.pos.y >= hero.pos.y)" vs. " if(enemy.pos.y > hero.pos.y)". I want to use " if(enemy.pos.y >= hero.pos.y)" just in case a yak ever spawned in the same y position as the hero. Why doesn’t it work?

The below code leads to a yak running me over when it’s on my y position.

// Keep moving right, but adjust up and down as you go.

while(true) {
    var enemy = hero.findNearestEnemy();
    var xPos = hero.pos.x + 5;
    var yPos = 17;
    if(enemy) {
        // Adjust y up or down to get away from yaks.
        if(enemy.pos.y >= hero.pos.y) {
            // If the Yak is above you, subtract 3 from yPos.
            yPos -=3; 
        } else if (enemy.pos.y < hero.pos.y) {
            // If the Yak is below you, add 3 to yPos.
            yPos +=3;
        }
    }
    hero.moveXY(xPos, yPos);
}

The below works as intended and I’m always able to complete the level, even when the yak spawns on hero’s position. I don’t understand why.

// Keep moving right, but adjust up and down as you go.

while(true) {
    var enemy = hero.findNearestEnemy();
    var xPos = hero.pos.x + 5;
    var yPos = 17;
    if(enemy) {
        // Adjust y up or down to get away from yaks.
        if(enemy.pos.y > hero.pos.y) {
            // If the Yak is above you, subtract 3 from yPos.
            yPos -=3; 
        } else if (enemy.pos.y < hero.pos.y) {
            // If the Yak is below you, add 3 to yPos.
            yPos +=3;
        }
    }
    hero.moveXY(xPos, yPos);
}

So, the default code uses >/< because if the Yak spawns at your yPos, you move back to the center of the line at yPos = 17. Then the hero decides which direction the Yak is and adjusts up or down.

In your >= case, if there is a yak is at your yPos at the bottom lane, you’ll remain in the same spot at yPos = 17 - 3 and eventually get hit.

1 Like