I want my enemy to attack the player, when they're near.
So I check their positions, and if they're near, the attack should happen.
I'm using a playerhit variable (until there isn't a better solution), because if I don't, the if condition will loop forever when I'm in the given area (the code is under the while(device->run()).. area).
This works for a single attack:
Code: Select all
if(enemy1health > 0){
playerpos = camera->getPosition();
enemy1pos = node->getPosition();
if(playerpos.X-enemy1pos.X < 0 && playerhit==0)
{
if(playerpos.X-enemy1pos.X > -110){
// the same as below
}
}
else if(playerpos.X-enemy1pos.X > 0 && playerhit == 0){
if(playerpos.X-enemy1pos.X < 110){
++playerhit;
controller.playAnimationNow("attack");
controller.playAnimation("walk");
playerhealth -= 33;
updateHealth(); // it displays the player's health depending on the playerhealth variable
}
}
}
If I use similar conditions after these, replacing playerhit==0 with playerhit==1, the second attack happens, but the health drops immediately twice as it should after one attack, even if I run out of the enemy's area before the second attack.
What am I missing?