Skip to main content
safiahn40752198
Known Participant
December 22, 2015
Answered

ActionScript 3 - Enemy-player collide, health bar decrease

  • December 22, 2015
  • 2 replies
  • 1829 views

//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?

This topic has been closed for replies.
Correct answer nezarov

You can use setTimeout() method with Boolean object:

//create the object on the main timeline

var onTouch:Boolean = false;

if ( player.hitTestObject(enemyList) &&  onTouch==false){

          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();

  }

2 replies

safiahn40752198
Known Participant
December 22, 2015

Thank you so much!!!

Inspiring
December 22, 2015

You're welcome.

safiahn40752198
Known Participant
December 22, 2015

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?

nezarovCorrect answer
Inspiring
December 22, 2015

You can use setTimeout() method with Boolean object:

//create the object on the main timeline

var onTouch:Boolean = false;

if ( player.hitTestObject(enemyList) &&  onTouch==false){

          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();

  }