Copy link to clipboard
Copied
//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?
You can use setTimeout() method with Boolean object:
//create the object on the main timeline
var onTouch:Boolean = false;
if ( player.hitTestObject(enemyList
onTouch=true;
trace("player collided with enemy");
currentHP -= 50;//health bar decrease
if(currentHP <= 0)
{
currentHP = 0;
trace("You died!");
}
else
{
setTimeout(function(){onTouch=false;}, 1000); // disable it after 1 second if the player still alive.
}
updateHealthBar();
}
Copy link to clipboard
Copied
You can use setTimeout() method with Boolean object:
//create the object on the main timeline
var onTouch:Boolean = false;
if ( player.hitTestObject(enemyList
onTouch=true;
trace("player collided with enemy");
currentHP -= 50;//health bar decrease
if(currentHP <= 0)
{
currentHP = 0;
trace("You died!");
}
else
{
setTimeout(function(){onTouch=false;}, 1000); // disable it after 1 second if the player still alive.
}
updateHealthBar();
}
Copy link to clipboard
Copied
Thank you so much!!!
Copy link to clipboard
Copied
You're welcome.
Copy link to clipboard
Copied
May I ask a question out of topic?
For scene 1
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
For scene 2
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
For Scene 1 and Scene 2 i used same name of variable and name of function.
Why it gives error?
Copy link to clipboard
Copied
Because it's duplicated variables & function. you can use the same variables in another scene without duplicating (you'll be able to access the scene1 variables from another scene) but you've to create new a function with a new name in the other scene.
Copy link to clipboard
Copied
Does it mean it is not necessary to declare variables of each scene? only need to declare in the first scene?
Copy link to clipboard
Copied
Yes.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now