Note: I know Inglor already covered this (and probably better than I will) But this is just the way I do it, and i find it easier to understand.
What You need To know
_currentframe function.
stop(); (like it was hard)
MovieClips
Variables
What you will learn
In this tutorial you will learn how to make a simple health bar for games. And anything else you might need to do bars for.
Starting
Well, first of all, make a bar. The Convert it into a symbol (F8), an MC. Go to frame 100 and insert a keyframe, then in frame 1, make the bar really small. And add a shape tween. Give it the instance name of "healtbar" without the quotes, add a stop action in it, and go back to the main scene. Now, say you already have the enemy and hero set up.
onto the first frame on the timeline, add this:
_root.healthbar.gotoAndStop(100);
//This will make it so that in the beggining, your health is 100. As stated in the healthbar.
This is just the way id go around it. Inside the enemy MC, theres gotta be an attacking animation. Go into it. Then search for something youd like it to hit (i.e. its hand if its punching) and add this code.
onClipEvent(enterFrame){
if(this.hitTest(_root.hero)){
_root.healthbar.gotoAndStop(_root.healthba
r._currentframe-=5);
}
}
What it does.
It checks that if the enemy's fist is hitTesting the hero, it will make the healthbar go and stop 5 frames before 100. In which the healthbar will be less.
Now, converting it into a variable:
First onto the main frame, add:
var Health:Number = 100;
This is just basic variable setting, theres no way you get lost.
Then you add this, again, in the same frame.
onEnterFrame = function(){
Health = _root.healthbar._currentframe;
}
this is also self explanatory, but heres what it does. It will make the variable health the same number as the currentframe of the healthbar MC.
Well thats about it, thats the way i do it. Any questions or comments, please post away!