PDA

View Full Version : health bar



colombianking
May 23rd, 2005, 04:52 PM
does anyone know how to make a health bar?
like if an enemy attackes you you lose life
and when it gets to zero you die:smirk:

Bone
May 23rd, 2005, 05:22 PM
What I did was to have a variable for your health which goes down when you are attacked.

This can be incorporated into the health bar by making a movie clip of the health bar in its various states going fom full to empty or vice versa. In this movieclip, you have some actionscript that will make you health bar go to the necessary frame for where the health variable is at. Something like:

onClipEvent (enterFrame){
if (_root.energy == 1){
gotoAndPlay(1)
}
}
onClipEvent (enterFrame){
if (_root.energy == 2){
gotoAndPlay(2)
}
}
Also, every frame of the movieclip should have 'stop();' in it.

Then you can use the same method to make the game go to the state it should be at when you die (when your health hits zero), something like this:

onClipEvent (enterFrame){
if (_root.energy == 0){
gotoAndPlay(gameover)
}
}

Hope this helps and isn't too badly worded. Let me know if you need more help.

Skribble
May 24th, 2005, 12:38 AM
thats a very overcomplicated way of doing it.

do this..



onLoad = function () {
life = 100;
hurt = false;
};
onEnterFrame = function () {
_root.lifeBar._xscale == life;
if(hurt == true){
life -= 10;
}
if (life <= 0) {
_root.hero.gotoAndStop("death");
}
};


firstly. It sets a variable called life and puts it too 100. Then you set the _xscale of the lifeBar to the health.

Next, if your hero is hurt it subtracts 10 from life. Now because you healthbars _xscale is set to the health variable, your healthbar will shrink 10.

Then if your life <= (less than or equal to) 0, you send your hero to the death frame. I use <= because its not always assured that your heros health will hit exactly 0.

Hope that helped.

colombianking
May 24th, 2005, 06:23 PM
that help but how do you make it for random damage because i know how to do it but what if i want it to go down a random number?:pac: :egg:

colombianking
May 24th, 2005, 07:20 PM
never mind i found a way thanks :beam:
thanks for the both of you :hugegrin: