ActionScript 3.0 - Health bar zero, go to game over
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=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?
