Dead enemies? Recognize? (Solved)


Is there a way to recognize if an enemy is dead? Because distanceTo(enemy) still calculates the distance to an enemy which is annoying when I have code saying if distanceTo(enemy)<10 I don’t do anything untill either another enemy spawns or the body dissapears. Solved you can close.


So the short answer is yes. You can do < 0 type of check…

if ( > 0)
        //enemy must be alive as they have more than 0 hit points

However (and this may not fit what you are doing, but I wanted to give a longer explanation, to help others)…

The reason you are still getting an distanceTo(enemy) result, is because you have not refreshed your enemy value. In other words the variable “enemy” is still pointing to that object (dead or not) that object has a vector and your character has a vector so there will always be a distance between them.

But a list of enemies “enemies=this.findEnemies();” would not return any dead enemies.
So If you do something like this…

    var enemies=this.findEnemies();
    var enemy=this.findNearest(enemies);
you could also do var enemy=this.findNearest(this.findEnemies());
at this point you now know that enemies and enemy do not contain any dead enemies
then you just need to make sure there is actually an enemy by doing
    if (enemy)
         //do stuff here

The bad side of the code above is that you may switch from enemy to enemy depending on which one is closer. In this case you would use the “if” statement from above to stay locked on to that particular enemy until his death or you could use a while loop.

while( >= 0)
    //do something about it


Aha thanks. I can’t believe I didn’t realize it doesn’t refresh XD
I guess you could put

if <1:
    distanceTo(enemy) +=100