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.
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);
}
}
}
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 ^^
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.
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: