I am totally new at this whole programming thing, and I really need help. So basically, I’m trying to make a healthbar that will increase or decrease depending on what button is clicked. I made a healthbar movieclip with 101 frames (I included zero) and put this in the actionscript layer of the movieclip:
var health:Number = 0;
if(health == 0)
{
gotoAndStop("1")
}
if(health == 1)
{
gotoAndStop("2")
}
if(health == 2)
{
gotoAndStop("3")
}
and on and on like so. Basically, on the stage itself, I have a button called fortyfiveup_btn that is commanded to do this:
var health:Number = 0;
fortyfiveup_btn.addEventListener(MouseEvent.CLICK, fortyfiveupClick);
function fortyfiveupClick(event:MouseEvent):void{
health = health+45
}
I quickly realized that both health variables, the one for the button and the one for the healthbar will not interact. How can I make it so if the button is clicked, the health goes to the relevant frame or percentage?
Thanks for any answers, and I appreciate all the help I can get 🙂
If the
answer == yesto my comment you should do this:You need to give the movieclip an instancename (perhaps lifebar) and from stage you can access the health inside the “lifebar” with
lifebar.health.So you need this inside your stage:
You can even optimize your lifebar script, don’t use 101 times the
if(health == x)you can use this, too:(I think this is inside an
ENTER_FRAMEevent?)EDIT:
Some error countermeasures:
Use
intinstead ofNumberwhen you don’t use decimal numbers anduintwhen you don’t use negative integers (this bugs when the number can drop under 0, so for your health we takeint):