Help with Thunderhooves! (javascript)


#1

Hi there!

I’ve been having some difficulties with the thunderhooves level. I just can’t figure out what I’m doing wrong! I’ve been getting a weird error saying “Unreachable ‘while’ after ‘return’.”

…help?

 loop {
    var yak = this.findNearestEnemy();
     if (yak) {
         // A yak is above you if it's y is greater than your y.
         // If the yak is above you, build a fence 10m below it.
         if(yak.pos.y < this.pos.y){
             var y = yak.pos.y + 10;
             var x = yak.pos.x;       
             this.buildXY("fence", x, y);            
          }
         if(yak.pos.y > this.pos.y){
             y = yak.pos.y - 10;
            x = yak.pos.x;
             this.buildXY("fence", x, y);
         }         
     }
    else {       
         var xx = this.pos.x + 10;
         var yy = this.pos.y;
         this.moveXY(xx, yy);
 }

 }

#2

Do you have a line like this at the top of your code?

return;  // Commented out to stop infinite loop

If so, it was inserted by the level-is-not-loading popup. You can delete that line.


#3

I can’t believe I didn’t see that before! Thank you :slight_smile:


#4

I’m also having problems with my code. When I run it, Anya doesn’t move at all…

loop {
var yak = this.findNearestEnemy();
if (yak) {
    var yakY = this.yak.pos.y;
    var yakX = this.yak.pos.x;
    if (yakY > this.pos.y) {
        this.buildXY("fence", yakX, yakY - 10);
    }
    if (yakY < this.pos.y) {
        this.buildXY("fence", yakX, yakY + 10);
    } else {
        var X = this.pos.x + 10;
        var Y = this.pos.y;
        this.moveXY(X, Y);
    }
}

}


#5

Your problem is this.yak.pos.y and this.yak.pos.x. They should just be yak.pos.y and yak.pos.x.


#6

I can’t believeI didn’t see that…Thank you!


#7

Hey, I am wondering what I am doing wrong, my hero is not moving at all, but I don’t get any " error" messages :confused: Thanks so much to anyone replying!!!

Edit: Got the error^^

loop {
    var yak = this.findNearest(this.findEnemies());
    if (yak) {
     
    var yakX = yak.pos.x;
    var yakY = yak.pos.y;
    
       if (yakY<this.pos.y) {
           this.buildXY("fence",yakX,yakY+10);
       }

     if (yakY>this.pos.y) {
         this.buildXY("fence", yakX, yakY-10); 
     }
         
 else {  
    

        var newX = this.pos.x+10;
        var newY= this.pos.y;
        this.moveXY(newX, newY);     
    }
}
}

#8

this code worked. I wasn’t able to figure out what I was doing wrong, but yours worked perfectly. :smile:


#9

Please help… I can’t get my char to put down more than one fence. Here’s my code

// Get to the oasis,
// fencing off paths with randomized yaks on them as you go.
loop {
    var yak = this.findNearestEnemy();
    if (yak) {
        var distance = this.distanceTo(yak);
        if (distance < yak.pos.y) {
            this.buildXY("fence", yak.pos.x , yak.pos.y - 10);
        }
         if (distance > yak.pos.y) {
            this.buildXY("fence", yak.pos.x, yak.pos.y + 10);
        }


        // A yak is above you if yak.pos.y is greater than your pos.y.
        
            // If the yak is above you, build a fence 10m below it.
        
            // If the yak is below you, build a fence 10m above it.
        
    } else {
    var newX = this.pos.x + 10;
    var newY = this.pos.y;
        this.moveXY(newX, newY);

        // Move right 10m towards the oasis.
        
    }
}

#10

This is wrong:

var distance = this.distanceTo(yak);
        if (distance < yak.pos.y) {

The comment hint for the level translates into:

// A yak is above you if yak.pos.y is greater than your pos.y
if(this.pos.y < yak.pos.y)

this.pos is your position. Generally, given an enemy, friend, item or flag, thingyName.pos is its position and thingyName.pos.x is the X coordinate


#11

Yeah I figured it out about 5 seconds after I posted this… Thanks for the advice though!


#13

@PokasLittleOne No one posts finished/working code here. We just post our bugged/not working code and then people offer suggestive hints to repair it in order to make the person think and learn. If we all posted our working/completed code, then no one would be forced to use their brain and learn anything. It is not helpful to just give people the answers. Please delete your post.


#14

All right that’s fine. It’s just that I know how difficult it can be to try and figure these things out, I know from personal experience. Just thought that I’d help give a reference code to help them see what they are doing wrong and try to fix it. But I see how that’s defeating the point, I won’t do it again.


#15

Hi everyone, i’m having troubles with this level too, my hero build first fence below the yak coming from top, then moving to the Oasis, but then he tries to build a fence behind the yak coming from below and I can’t figure ou why. I think it is because my hero consider the first yak as the nearest enemy when he spot the second one ? Here is my code, if you can give me an hint.

   while (true) {
    var yak = hero.findNearestEnemy();
    var xPos = hero.pos.x + 10;
    var yPos = hero.pos.y;
    if (yak) {
        yak.pos.y > hero.pos.y;
        hero.buildXY("fence", yak.pos.x, yak.pos.y - 10); 
    } else if (yak) {
        yak.pos.y < hero.pos.y;
        hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
    } else {
        hero.moveXY(xPos, yPos);
    }
}