ActionScript 3 - Enemy-player collide, health bar decrease
//declaration of variables
var maxHP:int = 100;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
//Here is the code when enemy-player collide
if (enemyList.length > 0){
for (var m:int = 0; m < enemyList.length; m++){
if ( player.hitTestObject(enemyList
trace("player collided with enemy");
currentHP -= 50;//health bar decrease
if(currentHP <= 0)
{
currentHP = 0;
trace("You died!");
}
updateHealthBar();
}
}
}
//update function
function updateHealthBar():void
{
percentHP = currentHP / maxHP;
healthBar.barColor.scaleX = percentHP;
}
The output for this code, the player only has two health bar, 100/50.
The problem is when the player hits enemy, the health bar decrease very fast, because of repeated hits as player through enemy (the size of enemy is as big as player).
What I want is when the player hits the enemy, the health bar decrease only once, even the player through the enemy. And when the health bar is 0 bar, game over, and the player needs to restart that level again.
So, how do I get it?
