Making a Health System
September 16th, 2006 by awesty
In this tutorial I will teach you how to make a Health System so when you character hits the enemy your health will decrease.
What you need to know:
- Basic flash skills. Like using all the tools and everything.
- You need to have taken my ‘Moving MovieClips with the ArrowKeys tutorial’. Click here veiw it now.
- It would also be good if you had taken my hitTest tutorial, but it doesn’t matter if you haven’t. Click here to veiw it now
- It would help if you had knowledge of another programming language like PHP, JavaScript or C++. Or even better know some ActionScript.
I am using Flash 8 for this tutorial, but you can use Flash MX 2004 if you want.
Lets begin!
Thanks for taking the time to veiw this tutorial. You can find more here. Please take your time to leave some feedback below
Filed under Flash Tutorials















how do i make a lose screen when the hp bar hits zero?
On the health bar change the code from this:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp < 0){
_root.hp = 0;
}
}
To this:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp < 0){
_root.hp = 0;
_root.gotoAndPlay(2)
}
}
And on the second frame add the lose screen.
ok yea i made the enemy have the health bar, and when you kill him it takes you to the secound page, and so i added another enemy on that page, but for some reason it skips that page and goes to the 3rd one, i am REALY lost and any help will be nice [oh and no error box comes up when i play the movie
Did you put:
stop();
on the second frame?
hello sir,
my problem is the healthbar:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp <= 0){
_root.hp = 0;
_root.gotoAndPlay(2);
}
}
i also used the _root.gotoAndStop(2)
inside the healthbar movievlip on the frame # 2 i put the win or lose screen, but it doesnt work when the healthbar turn to “0″ ?
awwwww SNAP
Great tutorial but there is an error in the script for the enemy mc. It should be as follows:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}
}
when you add the code for the lose screen you also need to add a stop action to frame 1:
stop();
Take two. Angle brackets are not allowed
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp (LESSTHAN) 0){
_root.hp = 0;
}
}
This is faster and more elegant (not that it matters much):
onClipEvent(enterFrame){
this._xscale = Math.max(_root.hp,0);
}
Still, good tutorials. Maybe I should write a few someday
It didnt work!!!!!
i dont know wat went rong!!!
Click on your enemy movieclip and put the actions with this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}
}
I accidentally left out a bracket in the tutorial, but stuler pointed it out to me.
I will fix the .fla tomorrow.
i have tryed 4 times.. it\’s donsen\’t work..if i root over the enmy there stil nothing happend..
On what frame or object do i put the _root.hp = 100; script?
how can i make a button that if “man” walk’s over it will change frame?
i figured it out… i added:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.gotoAndPlay(4)
}
}
to the finish “line”.
On what frame or object do i put the _root.hp = 100; script?
@rafel: Please explain your problem to me clearly.
@rok: You put that script on the first frame, it doesn’t matter which one as long as it is one of the first.
@haavard: Yep, that is right.
@Joe: Like I have already mentioned put it on the first frame.
OK, i’ve tried it with every possible variation i could think of but i cant get the health bar to decrease when man touches enemy, is there a .fla that you could post or e-mail me so i can see what i am doing wrong please?.
BTW Great tutorials, but this is the only one i cant seem to get right
i cant seem to get the enemy script right.
what is it?
other than that, cool tute… keep them coming.
do you have like a source file or something, that you can email me?
Try changing it to this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}
}
it says :
**Error** Scene=Scene 1, layer=main, frame=2:Line 3: ‘)’ expected
_root.hp -= (random(5) 1);
**Error** Scene=Scene 1, layer=main, frame=2:Line 5: Unexpected ‘}’ encountered
}
Total ActionScript Errors: 2 Reported Errors: 2
This is great. For class we are going to design a game, and I wanted to make a game to keep score by adding up the points, and this tutorial helped me to learn just that.
@sam:
Try changing
_root.hp -= (random(5) 1);
to
_root.hp -= (random(5)-1); (if there is no minus symbol there just pretend there is
)
Wonderfull! Tut! But one itsie pobs, how do i make it so say i have a movie clip called “upgrade” (the instance) when a movie clip called “shot” hits “upgrade” the hpbar goes up a bit?
put this code on upgrade:
onClipEvent(enterFrame){
if(this.hitTest(_root.shot)){
_root.hp = [NUMBER];
}
}
and replace number with whatever number you want to score to go up by. it is exactly the same code put some of the instance names are changed around and it is \’+\’ instead of \’-\’.
ive got my char to move wiv arrow keys,ive got him bumping into walls but i cant get the darn char to lose health its realy frustrating please e.mail me & tell me how to stop it sayin 2 errors & then loads of other stuff (=[
plz write bak soon by the way i reli need the info dude cus ive wanted to create a game cince i was like 4
Please email the .fla to me here and I will try to find the problem.
none of the enemy scipts work
*sigh*
i’ll have to use a different website
I can’t see any thing the movie is white
hey awesty good tut um… could u make a tut that sorta teaches the main points of a RPG or something like that or a tut on how to make a game like thing thing or something
thnx
kickass
@Bradosia: Are you sure you didn’t miss a step?
@P: Do you have the latest flash player?
@kickass: Yep, that will probably be my next tut.
NICE
um.. ok im confused??
about?
Wow! Just gotta work a bit with the scripts and it’s done!
Now my next step is to change the enemy and player to what I want them to look like!
I love it!
Great Tut(s) All Work Great! Will Definately Help Me On My Way To Learn Simple Game Design!
Is there a way to put a cap on the health bar so it wont raise above 100?
Yep.
if(_root.hp => 100){
_root.hp = 100;
}
Put that between the onClipEvent(enterFrame){} on any movieclip.
i have the same problem as many above…
i cant get the healthbar to go down when i “walk” over the enemy.. im using Flash Mx, not 2004, not pro.
Why won’t my movie work?
@Carl: Well it will only work with Flash MX 2004 and Flash 8 since they are the only versions that use ActionScript 2.0. The previous versions use AS 1.0 and the upcoming Flash 9 will have AS 3.0.
@m: I don’t know. Maybe if you explained it to me better I might.
nope…
**Error** Scene=Scene 1, layer=main, frame=2:Line 1: Statement block must be terminated by ‘}’
onClipEvent(enterFrame){
**Error** Scene=Scene 1, layer=main, frame=2:Line 4: Syntax error.
}
Total ActionScript Errors: 2 Reported Errors: 2
want me to send it to you? whats the address?
Tut works great, save the Last bit of AS.
The part with if (_root.hp
@Sam: admin[@]awestyproductions[.]com
@Shani: Huh?
It cut it off.
I ment to say the part with if
(_root.hp
.. And It keeps cutting it off.
The part of action script where the hp isn’t supposed to go to negitive numbers. When I copy and paste it from the tutorial and keep the man MC on the enemy, the numbers bounce from 0 to negitive numbers.
I have no clue whats wrong but I cant seem the get the hp bar to decreas umm i dont know your e-mail but i can send it to ya and mine is truedhar0k@aol.com the o is a zero.
This is what it is saying **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Unexpected ‘}’ encountered
}
Total ActionScript Errors: 3 Reported Errors: 3
I have tried over 10 times and added all the stuff in the forums if you can give me your email ill send it to ya.
ok i fixed some things so ill send you an updated version but still cant get bar to decrease. This is what its saying:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: ‘)’ expected
if(this.hitTest(_root.man){
Total ActionScript Errors: 1 Reported Errors: 1
Hey, I get a weird problem, I’m using Flash 8.
Alrighht, Everyuthing wroks fine, except when i move on him his health doesn’t lower. It says I have no errors but it still doesn’t work.
is there any way i could make a energy meter like it slowly goes up then when you use a energy attack it goes down depending wat lvl the attack is
@Chris: Add an extra bracket on.
if(this.hitTest(_root.man)){
@Jordan: Make sure you have all your instance names on the right MC. If you do send it to admin[@]awestyproductions[.]com and I will see the problem.
@Darin: Yes.
Set the energy up like the health (a bar and dynamic text). Give the text a variable name of energyTxt and put this on the bar:
onClipEvent(enterFrame){
this._xscale = _root.energyTxt;
if(_root.energyTxt < 100){
energyTxt = 2;
}
if(_root.energyTxt >= 100){
energyTxt = 100;
}
And put this on the frame:
energyTxt = 50; //So then it doesn’t start off full.
And then take this tutorial (link) and on the ‘man’ MC put this code.
if(this._currentframe == 3){
_root.energyTxt -= 10;
}
Put that between the onClipEvent(enterFrame)’{}’.
That is just off the top of my head so it might have a few bugs.
cooooooooooooooLLLLLLLLLLLLLLLLLLLLLLll
can you give me a code to make locked charecters that can be unlocked when u meet certain conditions
No, sorry. I might do a tutorial on it, but it would take a while so you might have to wait.
ok ill wait
I tried the code you created for the energy, but the energy bar neither decreases when the man moves, nor does it increase when the man is not moving.
Is it intended to do that?
If not, how could you make it do that?
I loved this tute, but I have to say that some things didn’t work. I use Flash 8, and have had no problems before with other tutorials. This one seems to be the first.
Ok, The bar doesn’t get smaller when I touch the enemy. Have I done anything wrong?
Can you make it so that if the health upgrade touches the “shot”, the upgrade dissapears?
So if I shoot the upgrade, my health goes up, then the upgrade dissapears?
@Goofy: Yes. I will try to do a tutorial for a simple shooter game.
@Jotte: Make sure you have all the instance names and variable names on the right thing, and make sure you have the codes on the right MC/frame.
@Jax: Just send the .fla to admin[@]awestyproductions[.]com
Thanks a ton, yo. But there’s still one prob.
Why does it show negative numbers? Didn’t the tute tell that It’s not supposed to do that?
It’s a great tute anyways
Put this code onto the health bar:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp
For < it won’t work unless you do this:
& l t ;
Without the spaces. (not in flash, in the comments).
What goofy tried to say was:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp <= 0){
_root.hp = 0;
}
}
cannot put enemy action error look
Clipboard Actions: Line 2: ‘)’ expected
if(this.hitTest(_root.man){
You are missing a ‘)’, just like it says.
Your code has 2 opening brackets and only 1 closing one.
Change it to this:
if(this.hitTest(_root.man)){
Hi,
Love the tutorial everythings working fine. I have a guy with a hammer and he can hit with his hammer when i press space bar. I want to know how if i have my big square and my guy hits the big square the health decreases.
Thanks
I was wondering - if you can make it go to and play frame 2, which has the game over screen, when your health reaches zero, is it possible to play another frame when your score reaches 1000? I have already created a scoring system., I just need to know how to link it to another frame.
Thanks.
@Jax: Put this on a movieclip:
onClipEvent(enterFrame){
if(_root.score >= 1000){
_root.gotoAndStop(3);
}
}
Just change score to whatever yours is called and on frame 3 put the win screen or whatever.
@Lachy: Read this tutorial and you should be able to.
Ah, I see.
I was trying to put this on a frame instead of a movie clip.
It worked, thank you.
sometimes when i try to watch the animation
i can’t it just dosen’t come up. so if you
could just write it out it might be easier
for people with slow internet to find out
what to do
Try getting the latest flash player and clear you cache or something because no matter how slow your computer is it should still work. It works on my 64mb RAM 54mb Internet laptop so it should work on most computers
hey.. my health bar has problem. the number will go beyond zero (negative) when it reaches zero and come back to zero again. can’t i just let it stay zero all the way?
i followed your tutorial very closely..
I done it on the first try… ~~
@Zack: There is no way to stop that from happening, but if you do this it will only happen once.
Change this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man){
_root.hp -= (random(5) 1);
}
}
To this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man){
if(!_root.hp <= 0){
_root.hp -= (random(5) 1);
}
}
i have the same problem as jordan… no errors but still the hp doesnt lower, ill check my mc’s. And could u tell me how to make a game that you can type your name in at the start, and then it can say for example… ” hello (your name) “
I will quickly exlpain it to you.
You need an input text box, give it a variable name of \\\’name\\\’ (without quotation marks), also put stop(); on the frame. Make a button underneath the input text box and make it go to the next frame. Make a dynamic text box and give it a variable name of output. Now on the second frame put this code:
stop();
_root.output = \\\”Hello \\\”+_root.name+\\\”!\\\”;
NOTE: Leave out the \\.
And thats it.
Now put this code on the frame:
_root.hp = 100;
What is the frame?
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Clip events are permitted only for movie clip instances
onClipEvent(enterFrame){
Total ActionScript Errors: 1 Reported Errors: 1
what does that mean and what do I do?
@Matt: Any of them, it doesn’t matter which layer, as long as it is the same as the dynamic text (the frame, not layer).
@Vikram: That means you put the code on a frame instead of a movieclip.
hey, i tested the program, but sometimes, when the box goes past the enemy, the health slightly increases. how can i fix this problem?
That shouldn’t be happening unless you put in a different code.
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 2: Operator ‘=’ must be followed by an operand
_root.output = ”Hello ” _root.name ”!”;
Total ActionScript Errors: 1 Reported Errors: 1
_root.output = “Hello “+_root.name+”!”;
hey,
regarding to a really old comment i sent you…
i have a text box on the first frame saying
‘whats your name?’
an input text box to type in your name(variable = name ) and an enter button.
on the next frame, there’s a dynamic text box (var = output) in which i want it to say ‘hello _your name_’
you’ve sent me this code for my second frame:
_root.output = “Hello “ _root.name ”!”;
but i always get this error:
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 2: Operator ‘=’ must be followed by an operand
_root.output = ”Hello ” _root.name ”!”;
Total ActionScript Errors: 1 Reported Errors: 1
please help!!
This comes up in a box called output
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 2: ‘)’ expected
if(this.hitTest(_root.man){
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 1: ‘;’ expected
oonClipEvent(enterFrame){
what does this mean?
i cant get this to work.
For the first once change it to this:
if(this.hitTest(_root.man)){
The second one to this:
onClipEvent(enterFrame){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: ‘)’ expected
_root.hp -= (random(5) 1);
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Unexpected ‘}’ encountered
}
Total ActionScript Errors: 2 Reported Errors: 2
this keeps comeing up wot shell i do or can u send me a copy of the flash thing so i can see and look at it my self
I am a junior hool student here but i am trying to make a game using flash mx. our school is teaching us how to do simple games. I am trying to make a dating game (something similar to sim girl or dna2) but I dont know how to make a health bar which is decreasing everytime the main character works to earn money. Anyone help me??? email me at lui_friend@yahoo.com
thanks!
@Daniel: Just send the .fla to admin[@]awestyproductions[.]com
@LOuiii: I don;t konw actionscript 1.0, which is what Flash MX uses. If you can grab hold of a cop of Flash MX 2004 or Flash 8 I could help you
/* This is for Flash MX,.. click the movie clip enemy and put this code. the enemy will react when the life is “0″ */
onClipEvent (enterFrame) {
if (_root.hp == 0) {
enemy = true;
_alpha = 75;
_rotation = 20;
_y = _y 26;
_x = _x 40;
}
/* the “9.5″ below refers to what damage you can give to enemy */
if (this.hitTest(_root.man)) {
_root.hp -= (random(1) 9.5);
}
}
I’m having the same problem as daniel i can’t figure out wat i copied wrong
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: ‘)’ expected
_root.hp = (random(5) 1);
Total ActionScript Errors: 1 Reported Errors: 1
hi plz will anyone help me i am a beginner to flash i have done that and i have put a square box in the bottom corner i want to be able to go over that ow with my object and it will take me to another frame IE level 2 so plz will any one tell me
ok, im sorry (your tutorials great!) but since its not changed theres ALOT of actionscript errors. It wontwork.
ok ifigured out what to do for the hpBar but the enemy script still doesnt work. Right now i have
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp