The mighty sand yak help


#1

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


#2

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);
        }
    }
}

#3

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 ^^


#4

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
help me, please…


#5

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)


#6

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.


#7

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.


#8

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


#9

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.


#10

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!


#11

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

Please leave dead threads dead; make a new thread if you need help.


#12

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…


#13
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?


#14

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).