0
votes

I currently have several movie clips and buttons on the stage that do different things. I have one button, that "Attacks" the enemy player and decreases his HP. This button has an event listener for a click, and when that is activated it goes through an IF statement and changes his health bar etc based on how low his health is. When the health reaches 0 I want to transition the entire screen to another ending screen.

I tried using .visible to make all of my other objects go invisible and that worked, however setting the instance button that I click to attack as not visible will not work. I have also tried removeChild, which won't remove the button, and gotoAndPlay/Stop to a future frame gives me a null object reference.

Here's the code for that specific button at that frame.

stop();

OSButton.addEventListener(MouseEvent.CLICK, OSAttack);

function OSAttack(event:MouseEvent):void
{
    var health1:int = parseInt(RegHealth.text);
    health1 = health1 - 1000;


        if(health1 == 9000 || health1 == 8000 || health1 == 7000 || health1 == 6000 || health1 == 5000
       || health1 == 4000 || health1 == 3000 || health1 == 2000 || health1 == 1000 || health1 ==0){
        REGHPBAR.play();
    }


    RegHealth.text = health1.toString();


    if(health1 <= 0){
        ////// WHAT CODE DO I PUT HERE? 
    }


}
1
visible or removeChild should work. If removing the object from the display list gives a null reference exception on a later frame, it must have been removed.Jason Sturges
as a huh? note.. what the 50 ors. if (health1 > 0) { REGHBNAR.play(); } else { removeChild(thingyThatDied); }Jason Reeves

1 Answers

0
votes

Try to use formatting with leading lowercase characters for your variable and function names and leading uppercase for class names. This is a common practice and will make reading your code easier.

When removing the button you should remove the listener as well. (look up and read about weak referencing as you might decide to start using this).

So your AS3 might look something like this:

oSButton.addEventListener(MouseEvent.CLICK, oSAttack);

//or using weak referencing
//oSButton.addEventListener(MouseEvent.CLICK, oSAttack, false 0, true);

function oSAttack(event:MouseEvent):void
{
var health1:int = parseInt(regHealth.text);
health1 = health1 - 1000;

if(health1 == 9000 || health1 == 8000 || health1 == 7000 || health1 == 6000 || health1 == 5000 || health1 == 4000 || health1 == 3000 || health1 == 2000 || health1 == 1000 || health1 ==0){
REGHPBAR.play();
}


regHealth.text = health1.toString();

if(health1 <= 0){
////// remove the button
oSButton.removeEventListener(MouseEvent.CLICK, oSAttack);
oSButton.parent.removeChild(oSButton);

//if you no longer need the button you can null it
oSButton = null;
}

}