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);
}
}
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.
}
}
@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.
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.
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);
}
}
can anyone help me?
// Get to the oasis,
// fencing off paths with randomized yaks on them as you go.
while(true){
var yak = hero.findNearestEnemy();
if (yak) {
var distance = hero.distanceTo(yak);
if (distance < yak.pos.y) {
hero.buildXY(“fence”, yak.pos.x , yak.pos.y - 10);
}
if (distance > yak.pos.y) {
hero.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 = hero.pos.x + 10;
var newY = hero.pos.y;
hero.moveXY(newX, newY);
}
I’m having trouble with the level. Does anyone think they could help? Here’s my code:
// Move right, to the oasis.
// Build a "fence" above or below when you see a yak.
while (true) {
var yak = hero.findNearestEnemy();
if (yak) {
// If yak.pos.y is greater than hero.pos.y
if (yak.pos.y > hero.pos.y) {
// buildXY a "fence" 10m below the yak.
hero.buildXY("fence", yak.pos.y - 10, yak.pos.x);
} // else:
else {
// buildXY a "fence" 10m above the yak.
hero.buildXY("fence", yak.pos.y + 10, yak.pos.x);
}
} else {
// moveXY right 10m towards the oasis.
hero.moveXY(hero.pos.x + 10, hero.pos.y);
}
}
The hero just ignores the first yak, then goes into the bottom middle pit and builds a fence there then gets killed. Any help is appreciated.