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

ActionScript 3.0 - Health bar zero, go to game over

Explorer ,
Dec 24, 2015 Dec 24, 2015

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?

TOPICS
ActionScript
1.0K
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
Community Expert ,
Dec 24, 2015 Dec 24, 2015

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

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 24, 2015 Dec 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

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

  }

  }

}

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 24, 2015 Dec 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.

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 24, 2015 Dec 24, 2015

Try like this:

if( bungaList.length > 0){

                             updateHealthBar2();

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

updateHealthBar2();

  gotoAndStop(1,"Game Over");

  }

  else

  {

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

  }

  }

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 24, 2015 Dec 24, 2015

Still gives error,

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

  at RECOVER_tettok_fla::MainTimeline/loop2()[RECOVER_tettok_fla.MainTimeline::frame1:335]

where frame1:335 is

if (player.hitTestObject(bungaList) && onTouch==false){}
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 24, 2015 Dec 24, 2015

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

            updateHealthBar2();

        }

    }

function updateHealthBar2(): void

{

    percentHP = currentHP / maxHP;

    healthBar.barColor.scaleX = percentHP;

   

    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.

    }

}

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 24, 2015 Dec 24, 2015
LATEST

still gives error,

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

  at Untitled_2_fla::MainTimeline/loop2()[Untitled_2_fla.MainTimeline::frame1:330]


frame1:330

if (player.hitTestObject(bungaList) && onTouch==false){}
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