Best viewed in Firefox

Awesty Productions

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 ;)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

278 Comments »

Comment by Andybob
2006-09-24 01:22:13

how do i make a lose screen when the hp bar hits zero?

 
Comment by awesty
2006-09-24 12:55:11

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.

Comment by Johnpinkertin Subscribed to comments via email
2007-10-21 13:37:17

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

Comment by awesty
2007-10-25 21:57:24

Did you put:

stop();

on the second frame?

 
 
Comment by tim Subscribed to comments via email
2007-12-24 00:24:04

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″ ?

 
 
Comment by Johny
2006-09-24 18:45:11

awwwww SNAP

 
Comment by stuler
2006-09-27 03:52:01

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);
}
}

 
Comment by stuler
2006-09-27 03:59:02

when you add the code for the lose screen you also need to add a stop action to frame 1:

stop();

 
Comment by Goot
2006-09-27 09:01:04

Take two. Angle brackets are not allowed :P

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 :P

 
Comment by Kayzar
2006-09-27 14:10:46

It didnt work!!!!!
i dont know wat went rong!!!

 
Comment by awesty
2006-09-27 18:49:43

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.

 
Comment by rafel
2006-10-01 20:38:42

i have tryed 4 times.. it\’s donsen\’t work..if i root over the enmy there stil nothing happend..

 
Comment by Rok
2006-10-04 07:11:11

On what frame or object do i put the _root.hp = 100; script?

 
Comment by haavard
2006-10-05 00:11:56

how can i make a button that if “man” walk’s over it will change frame?

 
Comment by haavard
2006-10-05 01:30:29

i figured it out… i added:

onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.gotoAndPlay(4)
}
}

to the finish “line”.

 
Comment by Joe
2006-10-05 16:39:16

On what frame or object do i put the _root.hp = 100; script?

 
Comment by awesty
2006-10-06 17:15:14

@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.

 
Comment by Rok
2006-10-07 05:12:02

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 :(

 
Comment by sam
2006-10-07 15:24:36

i cant seem to get the enemy script right.
what is it?

other than that, cool tute… keep them coming.

 
Comment by sam
2006-10-07 16:23:11

do you have like a source file or something, that you can email me?

 
Comment by awesty
2006-10-07 21:36:37

Try changing it to this:

onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}
}

 
Comment by sam
2006-10-09 16:09:59

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

 
Comment by tacotac
2006-10-10 01:10:57

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.

 
Comment by awesty
2006-10-10 17:11:51

@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 :P )

 
Comment by saminhood
2006-10-12 04:06:10

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?

 
Comment by awesty
2006-10-12 18:06:26

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 \’-\’.

 
Comment by angry joe
2006-10-21 05:40:51

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 (=[

 
Comment by angry joe
2006-10-21 05:50:22

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

 
Comment by awesty
2006-10-21 11:17:35

Please email the .fla to me here and I will try to find the problem.

 
Comment by Bradosia
2006-10-23 02:03:11

none of the enemy scipts work
*sigh*
i’ll have to use a different website

 
Comment by P
2006-10-23 02:23:25

I can’t see any thing the movie is white

 
Comment by kickass
2006-10-23 13:53:29

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

 
Comment by awesty
2006-10-23 16:41:51

@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.

 
Comment by Niceness
2006-10-24 00:06:52

NICE

 
Comment by rb
2006-10-24 06:27:21

um.. ok im confused??

 
Comment by awesty
2006-10-24 16:47:56

about?

 
Comment by Pamela
2006-10-25 14:48:50

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!

 
Comment by Murphy
2006-10-25 15:34:41

Great Tut(s) All Work Great! Will Definately Help Me On My Way To Learn Simple Game Design!

 
Comment by Sam
2006-10-25 15:35:54

Is there a way to put a cap on the health bar so it wont raise above 100?

 
Comment by awesty
2006-10-25 17:48:01

Yep.

if(_root.hp => 100){
_root.hp = 100;
}

Put that between the onClipEvent(enterFrame){} on any movieclip.

 
Comment by Carl
2006-10-26 17:57:31

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.

 
Comment by m
2006-10-27 07:34:53

Why won’t my movie work?

 
Comment by awesty
2006-10-27 10:09:23

@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.

 
Comment by sam
2006-10-28 11:41:21

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

 
Comment by sam
2006-10-28 11:43:02

want me to send it to you? whats the address?

 
Comment by Shani
2006-10-28 12:21:36

Tut works great, save the Last bit of AS.

The part with if (_root.hp

 
Comment by awesty
2006-10-28 13:10:13

@Sam: admin[@]awestyproductions[.]com

@Shani: Huh?

 
Comment by Shani
2006-10-29 09:23:03

It cut it off.

I ment to say the part with if
(_root.hp

 
Comment by Shani
2006-10-29 09:28:40

.. 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.

 
Comment by Chris
2006-10-30 01:14:54

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.

 
Comment by Chris
2006-10-30 01:39:44

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

 
Comment by Jordan
2006-10-30 08:35:48

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.

 
Comment by Darin
2006-10-30 12:35:26

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

 
Comment by awesty
2006-10-30 15:04:02

@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. ;)

 
Comment by hay man
2006-10-30 18:01:50

cooooooooooooooLLLLLLLLLLLLLLLLLLLLLLll

 
Comment by Darin
2006-10-31 04:40:09

can you give me a code to make locked charecters that can be unlocked when u meet certain conditions

 
Comment by awesty
2006-10-31 14:56:02

No, sorry. I might do a tutorial on it, but it would take a while so you might have to wait.

 
Comment by Darin
2006-11-01 09:01:50

ok ill wait

 
Comment by Jax
2006-11-01 18:46:42

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?

 
Comment by Jotte
2006-11-02 08:03:04

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?

 
Comment by Goofy
2006-11-02 09:40:14

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?

 
Comment by awesty
2006-11-02 16:17:17

@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

 
Comment by Jotte
2006-11-02 19:23:00

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 ;)

 
Comment by Goofy
2006-11-03 12:07:53

Put this code onto the health bar:

onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp

 
Comment by awesty
2006-11-03 15:04:35

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;
}
}

 
Comment by timmy
2006-11-04 14:05:59

cannot put enemy action error look
Clipboard Actions: Line 2: ‘)’ expected
if(this.hitTest(_root.man){

 
Comment by awesty
2006-11-04 14:29:55

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)){

 
Comment by Lachy
2006-11-05 11:41:22

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

 
Comment by Jax
2006-11-05 15:00:50

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.

 
Comment by awesty
2006-11-05 17:57:26

@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.

 
Comment by Jax
2006-11-05 18:22:13

Ah, I see.
I was trying to put this on a frame instead of a movie clip.
It worked, thank you.

 
Comment by flash freak
2006-11-08 14:22:52

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

 
Comment by awesty
2006-11-08 15:15:23

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 ;)

 
Comment by Zack
2006-11-08 19:31:50

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..

 
Comment by chong
2006-11-08 19:40:21

I done it on the first try… ~~

 
Comment by awesty
2006-11-09 15:42:12

@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);
}
}

 
Comment by sam
2006-11-09 20:02:43

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) “

 
Comment by awesty
2006-11-10 15:58:06

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. ;)

 
Comment by Matt
2006-11-11 04:04:37

Now put this code on the frame:
_root.hp = 100;

What is the frame?

 
Comment by vikram
2006-11-11 06:41:09

**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?

 
Comment by awesty
2006-11-11 10:53:37

@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.

 
Comment by vikram
2006-11-12 04:34:27

hey, i tested the program, but sometimes, when the box goes past the enemy, the health slightly increases. how can i fix this problem?

 
Comment by awesty
2006-11-12 09:50:51

That shouldn’t be happening unless you put in a different code.

 
Comment by sam
2006-11-13 15:09:08

**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

 
Comment by awesty
2006-11-13 16:00:29

_root.output = “Hello “+_root.name+”!”;

Comment by sam
2007-11-29 15:41:43

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!! :)

 
 
Comment by 0\/\/e|\|
2006-11-15 11:10:41

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.

 
Comment by awesty
2006-11-15 15:24:08

For the first once change it to this:
if(this.hitTest(_root.man)){

The second one to this:
onClipEvent(enterFrame){

 
Comment by daniel
2006-11-16 02:50:54

**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

 
Comment by LOuiii
2006-11-16 23:14:18

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!

 
Comment by awesty
2006-11-17 09:29:57

@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 ;)

 
Comment by jsph
2006-11-18 00:14:57

/* 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);
}
}

 
Comment by Karen
2006-11-22 07:39:09

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

 
Comment by tommo
2006-11-23 04:10:48

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

 
Comment by cool
2006-11-23 11:16:23

ok, im sorry (your tutorials great!) but since its not changed theres ALOT of actionscript errors. It wontwork.

 
Comment by cool
2006-11-23 12:30:50

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

 
Comment by cool