I don’t know what I’m doing wrong but it’s not working.Can I please have help fixing this?
def enemyInRange(enemy):
# Return true if the enemy is less than 5 units away.
if enemy < 5:
return True
else:
return False
def cleaveOrAttack(enemy):
if hero.isReady('cleave'):
hero.cleave(enemy)
else:
hero.attack(enemy)
while True:
enemy = hero.findNearestEnemy()
if enemy:
# Check the distance of the enemy by calling enemyInRange.
if enemyInRange(enemy):
cleaveOrAttack(enemy)
Hello, I had looked at your code, and I noticed your “enemyInRange” function is not doing what it should. That is because you are not using one command.
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
// Check the distance of the enemy by calling enemyInRange.
if (hero.enemyInRange(enemy)) {
hero.cleaveOrAttack(enemy);
}
}
}
def enemyInRange(enemy):
# Return true if the enemy is less than 5 units away.
enemy = hero.findNearestEnemy()
if hero.distanceTo(enemy)<5:
true = 1
return true;
else:
false = 0
return false
def cleaveOrAttack(enemy):
if hero.isReady(‘cleave’):
hero.cleave(enemy)
else:
hero.attack(enemy)
while True:
enemy = hero.findNearestEnemy()
if enemy:
# Check the distance of the enemy by calling enemyInRange.
if enemyInRange(enemy):
cleaveOrAttack(enemy)
Hey I was looking over this post because I was also confused, I got it to work on my own before I got to @Sirfirelords post by creating this function:
function enemyInRange(enemy) {
enemy = hero.findNearestEnemy();
// Return true if the enemy is less than 5 units away.
if (enemy) {
return true;
}else {
return false;
}
}
From my understanding what you had to do was define the variable enemy to find the nearest enemy with the code
enemy = hero.findNearestEnemy();
then you had to make an if statement that asks if it is true or not and if it is return true and if not return false.
Then if I understand the while loop correctly it checks to see if there is an enemy in range and under 5 units away and if that is true it will cleave the enemies and if there are are enemies but cleave is on cool down than it will default to regular attacking.
Hope this helps, I got through the level so I think I understand it feel free to correct me though or just let me know I was right!
Enemy isn’t a range. You might want to switch your code to:
while (true) {
var distance = hero.distanceTo(enemy);
if (distance < 5) {
return true;
} else {
return false;
}
}
}
The rest of your code was automatically generated, so you don’t need to change that! Hope this helps!