Flash ActionScript 2.0 Tutorial – Making a Health Bar – Part 2

This is part 2 of the “Making a Health Bar” tutorial series. If you’re just now starting and need to get to part 1 you can click the link in the resources to get there. Everyone else please read on.

onClipEvent(enterFrame){
this._xscale=_root.health*1;
if(_root.health_root.health=0;
}
if(_root.health>=100){
_root.health=100;
}
}

Isn’t that pretty? Now I know half of you are just going to
copy and paste and be on your way. That’s fine I don’t care
long as I was of some help for that is somewhat the reason for
this tutorial. But it is mostly, considering that it is indeed
a tutorial, meant for people who want to actually learn how to
do it and know why it works the way it does. Anyways lets go
line by line and I’ll explain what they all mean.

Line 1 – onClipEvent(enterFrame){

Nothing you ever do on a movie clip will ever work
without this so that’s reason number 1 why that’s there. I mean
there are other commands like onClipEvent(Load){ but that is
for another day. This command is telling it to check for
everything that you tell it to every time it enters the frame
that it’s in which in this case will be constantly cause we
will be in the same frame the whole time.

Now when you type this in and press Enter, you’ll
notice it’ll skip two lines and add a }. In between the } and
the { in the 1st line is where you want to put your commands.
For those of you who need to see it:

onClipEvent(enterFrame){
//this is where your commands go
}

Line 2 – this._xscale=_root.health*1

This is basically dividing your bar into numerous
sections so to speak. Its saying that if the variable “health”
goes down to 80. It will show it on the bar by reducing it to
80%. Now the bar is automatically on a scale of 100. So in this
case, since our health is 100, everything is easy. we simply
say that the bar’s scale = _root.health*1 meaning that
every time your health goes down by 1 the bar will go down by 1.
If you were to say your health is 100,000, it would be

this._xscale=_root.health*0.001

Now since your saying your health is 100,000, you’re telling it
that every time your health goes down by 1 your bar must go
down by .001 because its still on a scale of 100 and you want
them both to go down evenly and hit 0 at the same time. Makes
sense? I hope so.

The star in that line means multiply. What you want to do is
multiply your health by what ever number willl make it = 100 so
that it will stay even with your bar. so if your health was
500, it’ll say

this._xscale=_root.health*0.2

Simple right? Okay moving on.

Lines 3,4 and 5 – if(_root.health _root.health=0;
}

This is saying that if your health drops below 0, it will go
back to 0. If you forget to do this and later you make it so
when your health hits 0 it’ll go to a game over screen, it wont
do it. Your life will be 5 and your enemy will hit you for 10
and bring your life to -5 and therefore wont send you to the
game over screen. So yeah that’s all that is.

Line 6,7 and 8 – if(_root.health>=100){
_root.health=100;
}

This is the same as lines 3,4 and 5 except it’s so when your
health is full it won’t go any higher. Say you make a way for
your health to replenish. You make a potion that gives 50
health. Your health is 75 so when you use the potion it adds 50
and now your health is 125. Well the code above will stop that.
it’ll bring it back down to 100 where it should be.
Now let’s make a button to make sure the health bar works.
That’s a whole other tutorial all in itself so lets go to the
next tutorial. To continue please go to “Flash ActionScript 2.0 Tutorial – Making a Health Bar – Part 3/Making a Button”


People also view

Leave a Reply

Your email address will not be published. Required fields are marked *