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!
if (true) {
hero.attack(enemy);
}
}
what am i doing wrong i keep failing. after i kill 5 i die
I tried it, and I keep taking damage, because the character is moving toward the munchkins. Is there a way to fix it?
Here’s my code(Javascript):
// You are trapped. Don’t move, it’ll be painful.
// 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
var canAttack = inAttackRange(enemy);
// and save the result in the variable canAttack.
if(canAttack){
}
// If the result stored in canAttack is true, then attack!
hero.attack(enemy);
}
# You are trapped. Don't move, it'll be painful.
# This function checks if the enemy is in your attack range.
def inAttackRange(enemy):
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.
enemy = hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.
inAttackRange(enemy)
# If the result stored in canAttack is True, then attack!
if 'canAttack' 'true':
hero.attack(enemy)
hero.attack(enemy)
pass
Hi @floridamanog, welcome to the forum!
There are two problems here:
The comments say:
You’ve done the first bit right, but you needed to save the result in a variable called canAttack.
The inAttackRange(enemy) function returns either True or False. You need to put that value in a variable. inAttackRange returns a value like hero.findNearestEnemy() returns a value. So you need to put inAttackRange on the right of an equals sign like you did here:
But instead of enemy you would have canAttack.
Then, in your if statement, you would use the canAttack variable like you have, but without the quotes ‘’. That makes it a string: a message with doesn’t have any value. To check if something is stored in a variable you use ==. Like if enemy.type == “burl”:, but you need to check if canAttack equals True. Note there are no quotes around True, and it has a capital T.
I hope this helps,
Danny