Skip to main content
safiahn40752198
Known Participant
December 24, 2015
Question

ActionScript 3.0 - Health bar zero, go to game over

  • December 24, 2015
  • 2 replies
  • 1131 views

Initialize variables,

var bungaList:Array = new Array();

var maxHP:int = 90;

var currentHP:int = maxHP;

var percentHP:Number = currentHP / maxHP;

Functions,

function loop2(e:Event):void{

....

....

....

....

  bungaList.push(back.other.bunga1);

  bungaList.push(back.other.bunga2);

  bungaList.push(back.other.bunga3);

  bungaList.push(back.other.bunga4);

  bungaList.push(back.other.bunga5);

  bungaList.push(back.other.bunga6);

  bungaList.push(back.other.bunga7);

  if( bungaList.length > 0){

  for(var r:int = 0; r < bungaList.length; r++){

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

  onTouch=true;

  trace("player collided with enemy");

  currentHP -= 30;//health bar decrease

  if(currentHP <= 0)

  {

  currentHP = 0;

  trace("You died!");

  trace("restart level");

  removeEventListener(Event.ENTER_FRAME, loop2);

  gotoAndStop(1,"Game Over");

  }

  else

  {

  setTimeout(function(){onTouch=false;}, 1000); // disable it after 1 second if the player still alive.

  }

  updateHealthBar2();

  }

  }

function updateHealthBar2():void

{

  percentHP = currentHP / maxHP;

  healthBar.barColor.scaleX = percentHP;

}

The output is when the health bar become zero, cannot go to the next scene, which is game over.

Give errors,

TypeError: Error #1009: Cannot access a property or method of a null object reference.

  at Untitled_3_fla::MainTimeline/updateHealthBar2()[Untitled_3_fla.MainTimeline::frame1:578]

  at Untitled_3_fla::MainTimeline/loop2()[Untitled_3_fla.MainTimeline::frame1:346]

and at line 578 is

healthBar.barColor.scaleX = percentHP;

and at line 346 is

updateHealthBar2();

How to solve this?

This topic has been closed for replies.

2 replies

Inspiring
December 24, 2015

Try:

for(var r:int = 0; r < bungaList.length; r++){

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

  onTouch=true;

  trace("player collided with enemy");

  currentHP -= 30;//health bar decrease

                                       updateHealthBar2();

  if(currentHP <= 0)

  {

  currentHP = 0;

  trace("You died!");

  trace("restart level");

  removeEventListener(Event.ENTER_FRAME, loop2);

  gotoAndStop(1,"Game Over");

  }

  else

  {

  setTimeout(function(){onTouch=false;}, 1000); // disable it after 1 second if the player still alive.

  }

//  updateHealthBar2(); remove this line from here

safiahn40752198
Known Participant
December 24, 2015

If i put updateHealthBar2(); , it will gives errors.

I forgot that you already told me before. When i tried, it works.

Thank you.

if (enemyList.length > 0){

    for (var m:int = 0; m < enemyList.length; m++){

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

          onTouch=true;

  trace("player collided with enemy");

  currentHP -= 30;//health bar decrease

  // updateHealthBar2();

  if(currentHP <= 0)

  {

  currentHP = 0;

  setTimeout (function (){    endpoint=true;

  trace("You died!");

                            removeEventListener(Event.ENTER_FRAME, loop2);

                            gotoAndStop(1,"Scene 2");}, 1000);

  }

  else

  {

  setTimeout(function(){onTouch=false;}, 1000); // disable it after 1 second if the player still alive.

  }

  updateHealthBar2();

  }

  }

}

Inspiring
December 24, 2015

I don't think you need that, the error comes because you're leaving to another scene before the function being completed, I think that you've to update the HealthBar in somewhere else before you go to another scene.

Joseph Labrecque
Community Expert
Community Expert
December 24, 2015

I'd imagine either currentHP or maxHP are not defined previous to the percentHP assignment.