# Mighty Sand Yak help

#1
``````enemy = self.findNearestEnemy()
loop:
if self.distanceTo(enemy) < 10:
self.moveXY(x, y)

x = self.pos.x + 10
y = self.pos.y
``````

Can someone help me how to do this level thanks im stuck…

#2

Well, you have tried to move to X and Y even though you don’t have x and y defined yet. you need to define x and y before having the:

``````self.moveXY(x, y)
``````

#3

My character is not moving at all, but there are no errors.

Code:

``````enemy = self.findNearestEnemy()
if enemy < 10:
x = self.pos.x + 10
y = self.pos.y
self.moveXY(x,y)
else:
pass``````

#4

Try this

``````enemy = self.findNearestEnemy()
if enemy :
if distanceto(enemy) < 10:
x = self.pos.x + 10
y = self.pos.y
self.moveXY(x,y)
else:
pass``````

#5

Thanks i got it to work!

``````loop:
enemy = self.findNearestEnemy()
if self.distanceTo(enemy) < 10:
x = self.pos.x + 10
y = self.pos.y
self.moveXY(x,y);
else:
pass
``````

#6

Still doesn’t work. the error is if distanceto(enemy) < 10:

#7

#8

For this I used pure luck because I was really stuck on the level, so I just ran to the end. If all else fails, try this method, but don’t expect it to work the first time, as the yaks come in random order.

#9

self.distanceTo(enemy)

not

distanceTo(enemy)

#10

I’m extremely confused, why do people all have self. And I have this. When it comes to commands

#11

Us who have `self` mostly use Python. You most likely are using a different language, such as JavaScript.

#12

Just so you know, I believe self (in Python) and this (in Javascript) are equivalent–they let the computer know you want your own character, not another, to use the method.

#13

Not really “equivalent”…

(simplified explanation)

this in javascript always refers to the current running function. So when you are writing main hero code, “this” is the “hero” object, but once you get to writing subfunctions, then inside the subfunction “this” will be the subfunction and not the “hero”. (unless written a special way so as to keep this --> hero, which is the way I believe CoCo teaches you to write javascript subfunctions.)

self in python always refers to the main “body/object of code” (the hero) even when in a subfunction you have written.

#14

// Let yaks get close, then move 10m right to dodge.
// Dodge 4 yaks to complete the level.

loop {
// Use “if” to only move when a yak is less than 10m away.
var enemy = this.findNearestEnemy();
var distance = this.distanceTo(enemy);
if
// Use your Sense Stone to access this.pos.
var x = this.pos.x + 10;
var y = this.pos.y;
this.moveXY(x, y);
}

This is all i gotten so far i just dont know what to do afetr if im absolutely lost.

#15

Please format your code by highlighting it and clicking the “</>” button. While I don’t do Javascript, I do notice that your “if” statement is blank.

#16

If is blank because thats as far as ive gotten. also i have no idea how to highlight it so i just posted it. i just dont know what to do after IF

#17

Hello, TreeioSmith, and welcome. For future reference, you can format your code properly as Gundericus suggested or by reading the FAQ.

Well, first you have to check for an enemy, right? You can’t use `enemy` if there isn’t one.

#18

But i did put the code to identify an enemy

#19

Im struggling to figure out what i put after If

#20

Have you read the Help? Do you have the Polished Sense Stone? How will you tell when a yak is less than 10 meters away? Here’s a hint: distanceTo returns the distance in meters.