Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ActionScript 3 - Enemy-player collide, health bar decrease

Explorer ,
Dec 21, 2015 Dec 21, 2015

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

TOPICS
ActionScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 21, 2015 Dec 21, 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();

  }

Translate
Enthusiast ,
Dec 21, 2015 Dec 21, 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();

  }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 21, 2015 Dec 21, 2015

Thank you so much!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 21, 2015 Dec 21, 2015

You're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2015 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 22, 2015 Dec 22, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2015 Dec 22, 2015

Does it mean it is not necessary to declare variables of each scene? only need to declare in the first scene?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 22, 2015 Dec 22, 2015
LATEST

Yes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines