Can someone help in Sarven Road? (Javascript)


#1

// Get to the oasis. Watch out for new enemies: ogre scouts!
// Go up and right by adding to your current X and Y position.
var enemy = this.findNearestEnemy();

loop{
if (enemy) {
this.moveXY(x, y);
}

// Attack any enemies you see, or keep moving up and right.

var x = this.pos.x + 10;
var y = this.pos.y + 10;

else {
  if (this.isReady("cleave")) {
     this.cleave(enemy);  
   }
else if (enemy){
    this.attack(enemy);
}  

}

}
my player won’t attck


#2

Seems to me it should be giving you an error…

you have:

if { ... }
stuff
else {...}

the else is not attached to any if.


#3

oh I get it but my player still won’t attack it just goes to the oasis

// Get to the oasis. Watch out for new enemies: ogre scouts!
// Go up and right by adding to your current X and Y position.
var enemy = this.findNearestEnemy();
if (enemy){
var distance = this.distanceTo(enemy);
}
loop{
if (!enemy) {
var x = this.pos.x + 10;
var y = this.pos.y + 10;
this.moveXY(x, y);

// Attack any enemies you see, or keep moving up and right.

}
else if (distance < 10) {
if (this.isReady(“cleave”)) {
this.cleave(enemy);
}
else {
this.attack(enemy);
}
}

}


#4

enemy is always null, as it’s not being updated in the loop.

Also protip: cleaving scouts with the Long Sword is somewhat useless, as it takes too long and doesn’t inflict a lot of damage on the scouts. (Normal attack is fine, though.)


#5

So what can I do to have it updated in the loop?


#6

Move the loop keyword such that it wraps all of your logic…

loop {
    // find enemy
    // attack enemy if enemy else move
}

#7

But I don’t know how do that. Can you show me or give me a hint?


#8

In the forest levels, where did you put var enemy = this.findNearestEnemy() in your code?

Just do the same with this level.


#9

You pasted and posted:

var enemy = this.findNearestEnemy();
if (enemy){
var distance = this.distanceTo(enemy);
}
loop{

trotod said:

So all you have to do is “cut” and “paste” the loop line to before the code that “finds” the enemy…(you know “enemy =”…)