Do you remember this?
not really:small_airplane:
In this case, go back to older levels to review what you have learned. Maybe the previous 5 - 10 levels. Then, apply those skills to this level.
i did that and i don,t now what to do:cry:
Let’s break it down a little bit for you:
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.
Call inAttackRange(enemy): You want to trigger the function that has been defined in the code at the top. You call this by typing in the function with an argument in the ( )'s. The argument it asks you is to place in the ( )'s is “enemy”.
Save as a variable: saving a variable is what the “=” is saved for.
Example: x = 5
In this example you’re saving ‘5’ to the variable ‘x’.
With this information we know the variable we want is ‘canAttack’, we use an ’ = ’ sign to define the variable. For the variable we want to call ‘inAttackRange()’ and for the argument in the ()'s place in ‘enemy’.
So the final result would be:
canAttack = inAttackRange(enemy)
After you have this you can then call and ‘if/else’ statement to make you’re character attack the enemy, you’ve done this already plenty times.
i did that hellenar
i understand but i want to show my code to you
// This function checks if the enemy is in your attack range.
function inAttackRange(enemy) {
var distance = hero.distanceTo(enemy);
// Almost all swords have attack range of 3.
if (distance <= 3) {
return true;
} else {
return false;
}
}
// Attack ogres only when they’re within reach.
while (true) {
// Find the nearest enemy and store it in a variable.
var enemy = hero.findNearestEnemy();
// Call inAttackRange(enemy), with the enemy as the argument
// and save the result in the variable canAttack.
var canAttack = inAttackRange(enemy);
// If the result stored in canAttack is true, then attack!
hero.attack(enemy);
}