# The mighty sand yak help

Hey guys,
I hope someone can help me. I got stuck on the mighty sand yak.
Every time i submit, my character immediately starts walking, without waiting for an enemy entity to close in.
my current code is this:

``````loop {
enemy = this.findNearestEnemy();
distance = this.distanceTo(enemy);
if (enemy)
if (distance < 10)
this.pos = {x: 15, y: 30};
x = this.pos.x + 10;
y = this.pos.y;
this.moveXY(x,y);
}
``````

Am i overlooking something really simple here? Please tell me if so.

Thanks,
Stijn

You are missing some curly braces for one. two, you dont need to set this.pos itâ€™s a built in property that contains wherever your hero is currently located.

``````loop {
enemy = this.findNearestEnemy();
distance = this.distanceTo(enemy);
if (enemy) {
if (distance < 10) {
x = this.pos.x + 10;
y = this.pos.y;
this.moveXY(x, y);
}
}
}
``````
1 Like

Thanks! I completely forgot the curly braces after the â€śifâ€ť statements.
Also, thanks for telling me about the this.pos. I thought I would have to put that in every time as well.
Thanks you for your quick response ^^

Here is my code

loop {
ya=this.findNearest(this.findEnemies());
dis=this.distanceTo(ya);
if(ya){
if(dis<10){
x = this.pos.x + 10;
y = this.pos.y;
this.move({x:x, y:y});
}
}
}

but it doesnâ€™t work. I mean, every time I run code yak is killing me

What you are doing wrong I think is this part:

That statement is probably invalid, because you are trying to use a function in a function

``````(find..(find))
``````

Try switching that statement with this one:

``````ya = this.findNearestEnemy();
``````

that should make the yak an identifiable object.

Edit:
Also, try to delete the :x and :y in your this.moveXY
Just let the statement be this.moveXY(x, y)

No, this is valid, because I bought some special item. There isnâ€™t a problem. I discovered that I was too slow, thatâ€™s why I couldnâ€™t pass this level.

Actually `this.findEnemies()` returns a table, so `this.findNearest(...)` should work. The problem is in my opinion the x:x and y:y.

Nevertheless is `ya = this.findNearestEnemy()` much better to read, this function is there for a reason.

As others have said. no this is very valid and preferred in my opinion. Cleaner code

1 Like

Youâ€™re right. The position is a JSON-Object. Without the quotes, the x and y are parsed as variables, not as strings. The result will be something like this:

``````{
x: x,  // "this.pos.x + 10 as string": this.pos.x.+10,
y: y   // "this.pos.y as string": this.pos.y
}

// Result:
{
20: 20,
10: 10
}
``````

As you can see the object doesnâ€™t have the properties x and y. Try this instead:

``````{
"x": x,
"y": y
}

// Result:
{
"x": 20,
"y": 10
}
``````

Edit: The editor does accept both solutions.

As far as I can tell the `this.move` action is bugged. The loop does not wait until the action ends, unlike `this.moveXY`.

I have python coding or something and I donâ€™t know whatâ€™s wrong with my coding!

loop:
self.moveXY(10 , 30)
self.moveXY(70, 29)
self.moveXY(39, 28)

It says that I didnâ€™t dodge the 4 yaks when I actually did!

Hello, Nubsoldier, and welcome. Please read the FAQ on how to properly format code.

You didnâ€™t â€śdodgeâ€ť the yaks, you ran past them before they get close enough to â€śdodgeâ€ťâ€¦

IOW, if you donâ€™t do the level as instructed, you only get lucky if it lets you succeedâ€¦

``````loop:

if self.distanceTo(enemy) < 10:
self.moveXY(10 , 30)
self.moveXY(70, 29)
self.moveXY(39, 28)
``````

This is my code so far, can I get help?

The best help that can possibly be given is to suggest that you reload the level, read and follow the directions.

Because, what I said in my previous post applies to you as well, you are doing exactly what the previous person did (iow: not follow directions).