Attacking dead enemies


#1

Okay dunno if this is a bug but when I use the following code:

while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)

what happens is that my character simply keeps attacking a single enemy even when its hp has dropped to zero.
where this becomes a problem is in levels where you need to attack different enemies.
One example would be Village Rover
If I’ve simply written the code wrong then could someone tell me what I’m doing wrong?


#2

do you have a screenshot?


#3

The code is working fine for me, but it may be your indentation. Please use three backticks (or highlight the code and press the < / > button) when posting code, so that we can see your indentation. I just used this code (in Python) on Village Rover. It works fine:

while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)

You might consider using if enemy and enemy.health > 0: instead


#4

yo tayler can you help me, let me make a Topic


#5

I see that you said “where this becomes a problem is in levels where you need to attack different enemies.” Are you saying the level that your doing now only has one enemy? If so then your code would work as long as new enemies are appearing.


#6

sorry about not posting earlier. managed to find the problem myself and fixed it. Kinda hard to explain what the problem was but I managed to solve it.


#7

It’s okay son, It’s better when you find solution yourself.


#8

You know what they say “a picture is worth a thousand words.” When left speechless take a picture. :smiley:


#9