[Help] Basin Stampede (JavaScript)

Objective

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

while(true) {
    var enemy = hero.findNearestEnemy();                           //Find the Nearest Enemy
    var xPos = hero.pos.x + 5;                                     //Set the heroes postion to his X + 5 the way he will walk to the right?
    var yPos = 17;                                                 //Set yPosition to 17
    var subtractYPos = yPos - 3;                                   //Set new y position to current Y position - 3
    var addYPos =  yPos + 3;                                       //Set new y position to current Y position + 3
    if(enemy) {
        // Adjust y up or down to get away from yaks.
        if(enemy.pos.y > hero.pos.y) {                             //If the enemy position is > hero position
            // If the Yak is above you, subtract 3 from yPos.
            hero.moveXY(xPos, subtractYPos);                       //Hero will move along same X position but the new Y Position in this cas current Y - 3
        } else if (enemy.pos.y < hero.pos.y) {                     // Else hero will
            // If the Yak is below you, add 3 to yPos.
            hero.moveXY(xPos, addYPos);                            //Hero will move along the same X position but to the current Y postion plus 3
        }
    }
    hero.moveXY(xPos, yPos);
}

What is happening when I run the code:

I don’t understand what I am doing wrong at all I thought my code makes perfect sense. So any help is much appreciated.

it looks like the character is responding the first and second yak and then before he returns to the center position (y 17).he runs into the third yak. Try limiting the yaks he responds to by limiting the distance to which he will take action. I did it by adding an if statement after if enemy. If hero.distanceTo(enemy) < 5:

This way he ignores yaks that are too far away to be of any consequence. Everything else you have looks right although where you have a 5, I put a 10 to move him along faster.

I got it to work by just deleting the entire code and starting from scratch I made this new code and it worked

while(true) {
    var enemy = hero.findNearestEnemy();
    var  x = hero.pos.x + 5;
    var y = 17;
    if(enemy && enemy.pos.y > hero.pos.y) {
        hero.moveXY(x, y - 3);
    } else if (enemy && enemy.pos.y < hero.pos.x) {
        hero.moveXY(x, y + 3);
    } else {
        hero.moveXY(x, y);
    }
}

But can someone explain why it doesn’t work if the code is written this way? Isn’t it the same exact thing I just have it in a different order? Or does the order really matter.


while(true) {
    var enemy = hero.findNearestEnemy();
    var  x = hero.pos.x + 5;
    var y = 17;
    if(enemy && enemy.pos.y < hero.pos.y) {
        hero.moveXY(x, y + 3);
    } else if (enemy && enemy.pos.y > hero.pos.x) {
        hero.moveXY(x, y - 3);
    } else {
        hero.moveXY(x, y);
    }
}

Luck. else if(enemy && enemy.pos.y > hero.pos.x) You’re comparing pos.y to pos.x

Note this level you’re meant to do:

yPos += 3;
// Other if statement
yPos -= 3;

Not move twice in the same while-true loop, which is why your original code was failing.

Wow! I didn’t even realize I made that mistake, but changing it to += and -= did fix it for me. But why doesn’t y + 3 or y - 3 work. Isn’t the same thing happening? Or does this just add + 3 then I have to go back to my default location which is 17 because Y was never set to the new value after 3 was added? So then += will take the current value of Y add 3 to and then change the current y to the new y making it 20 instead of 17? Then depending on where a yak is coming from I will either add or subtract from the new current Y which would be 20?

y + 3 or y - 3 doesn’t work just because of the language you are using. If you were using Java Script you would have to use += or -= in a math situation unless you used or created a different language it is just how Java Script works.

y + 3 or y - 3 does work. It doesnt work the way he has it because hes not reassigning the value to y.
hero.moveXY(x, y - 3) #This doesnt change the value of y
y += 3 #Changes the value of y.

Both statements work but they are doing 2 differnt operations.

y = 17
hero.say(y - 3) #14
hero.say(y) #17

y= 17
hero.say(y-=3) #14 #Note this statement does not work I just wanted to show what its was essentialy doing.
hero.say(y)#14

Thanks guys I think I understand it now.

thank you, this code really help! I still dont understand why it has to be soooo picky! :grinning: