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