Be a Supporter!

As: Another Way About Healthbars

  • 5,022 Views
  • 27 Replies
New Topic Respond to this Topic
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
As: Another Way About Healthbars 2005-12-25 13:03:28 Reply

T3h Almighty AS: Main

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!

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Another Way About Healthbars 2005-12-25 13:12:34 Reply

Nice althought kinda simple.
I personally think that the other tutorial is better sry.
Not that this tutorial is bad just the other way is better.


BBS Signature
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As: Another Way About Healthbars 2005-12-25 13:17:02 Reply

At 12/25/05 01:12 PM, -Snail- wrote: Nice althought kinda simple.
I personally think that the other tutorial is better sry.
Not that this tutorial is bad just the other way is better.

yeah Inglor's. Using the width thing you mean.

Well this is how ive been making them for a while, and it works very good. When i was a n00b i didnt understand inglor's tutorial, so i made my own way.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Another Way About Healthbars 2005-12-25 13:21:27 Reply

Yeah, hopefully this can be in the AS list for newbies ;)


BBS Signature
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As: Another Way About Healthbars 2005-12-25 13:22:02 Reply

At 12/25/05 01:21 PM, -Snail- wrote: Yeah, hopefully this can be in the AS list for newbies ;)

yes. I finally got around to post something. I might post more stuffs later.

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Another Way About Healthbars 2005-12-25 13:23:16 Reply

At 12/25/05 01:17 PM, Xmas_Blaze wrote: yeah Inglor's. Using the width thing you mean.

my version of health bars is similar to both of these. i use inglors method of variable declaration, but i make an MC with 100 frames or whatever like your method. then i just say something like:

healthMC.gotoAndStop(Math.round(101-health
));


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Another Way About Healthbars 2005-12-25 13:24:04 Reply

At 12/25/05 01:03 PM, Xmas_Blaze wrote: What You need To know
_currentframe function.

it's not a function, it's a read only property in the MovieClip class

stop(); (like it was hard)

now that's a form of function (method)

MovieClips

you don't need to know "MovieClips" you need to know clip events

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.

now you realise that wastes a 100 frames, a mathed tween and a lot of other things, right?

_root.healthbar.gotoAndStop(100);

assuming it's on the stage root that is.

//This will make it so that in the beggining, your health is 100. As stated in the healthbar.

wouldn't the enterFrame do that automatically anyway in the enterFrame? oh, well, you're using gotoAndPlay and _currentframe instead of a hp or health variable,had you used one it would have been better memory-wise and would produce easier manipulation. a health=100 and then setting it to health would be cool, but you also forgot the entire linear side of it, in this tutorial health HAS to be maxed at 100 and mined at 0 for it to work

onEnterFrame = function(){
Health = _root.healthbar._currentframe;
}

you just did it reversed, variables are easier to process, are not read only and consume less CPU than accessing a movieclip's read only property and calling a function

Well thats about it, thats the way i do it. Any questions or comments, please post away!

you forgot the great potential of this concept, in this you don't have to use bars, but you can use any form of animation that swings between "healthy" and hurt, you can use simple precentage calculations to make sure it snaps right even in scales other than 1 and 100 on both linear increasments (health 1 to 100, bar 1 to 100), with this method you can use animations for being hurt (in movieclips in the frames) and a handful of other ideas (like the hurt face in "doom")

Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As: Another Way About Healthbars 2005-12-25 13:28:49 Reply

At 12/25/05 01:24 PM, Inglor wrote: now you realise that wastes a 100 frames, a mathed tween and a lot of other things, right?

I know it does Inglor, please dont kill me. :) Its just i couldnt understand your method way back, i can now. But ill still stick with this.

assuming it's on the stage root that is.

of course.

wouldn't the enterFrame do that automatically anyway in the enterFrame? oh, well, you're using gotoAndPlay and _currentframe instead of a hp or health variable,had you used one it would have been better memory-wise and would produce easier manipulation. a health=100 and then setting it to health would be cool, but you also forgot the entire linear side of it, in this tutorial health HAS to be maxed at 100 and mined at 0 for it to work

Well, i did set a variable. But I dont know what you mean by the last part of it.

you just did it reversed, variables are easier to process, are not read only and consume less CPU than accessing a movieclip's read only property and calling a function

oooh. Ok Inglor, ill remember that.

(like the hurt face in "doom")

Actually, you can still do that. Just check for numbers between the health variable, and make it like "gotoAndStop" -ish.

Well thanks Inglor, just dont kill me. Im a n00b. :)

Mad-Mardigan
Mad-Mardigan
  • Member since: Mar. 6, 2006
  • Offline.
Forum Stats
Member
Level 15
Animator
Response to As: Another Way About Healthbars 2006-06-13 20:14:54 Reply

... I want to make a first person fighting game and im a bit confused...
I did every thing with the health bar, but were do I go from there?
Do I make another for the enemy? How would I work with the buttons?

johnfn
johnfn
  • Member since: Aug. 16, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:24:52 Reply

My healthbars use just 3 lines of code.

onClipEvent(enterFrame){
this.healthbar._xscale = this.health/this.maxhealth;
}


[6,11,4,10,2,10,-68,5,15,-68,16,4 ,1,-68,-2,1,15,16,-67].map(function(v){ return String.fromCharCode(v + 100) }).join(""); // updated for web 2.0!

BBS Signature
Bleiz
Bleiz
  • Member since: Nov. 12, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:26:58 Reply

At 6/13/06 08:14 PM, Killer_Kadoogan wrote: ... I want to make a first person fighting game and im a bit confused...
I did every thing with the health bar, but were do I go from there?
Do I make another for the enemy? How would I work with the buttons?

You'd use hitTests and decrease a variable, or in this case, make the frame of the healthbar go and stop to another frame. As i stated in this tutorial.

This seems rather complicated, the way johnfn posted is much easier. Its covered on Inglor's tutorial. I was such a n00b when i posted this.

pixelz
pixelz
  • Member since: Feb. 19, 2006
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:35:18 Reply

Nice tutorial, but I had started a game a few weeks ago and all development came to a stop, I had finished it all up basically, and I had the health bar working the enemies were all perfect and all that.. BUT!.. when the health bar reached 0 it would just go back to full, I had tried numerous codes on trying to get it to go to the dead fraem but nothing was working. I swear I moved little bits like certain letters so then it would automatically go to the dead frame as soon as you began to play then I would replace the little letter of code or reverse it and it would do the same thing. Its just not working and whats a game if you can't die?


Whoa its me!

MARINO
MARINO
  • Member since: Feb. 21, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:42:25 Reply

Haha DFB, your first post on this thread reminds me of myself right now.

It's not bad actually... it's kind of nice that you posted your own way of doing things even though the other way may be easier. :)
xWELSHxDRAGONx
xWELSHxDRAGONx
  • Member since: Apr. 13, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:44:13 Reply

see now - the way i see it is AS:Main is only usful if you are n00b, with exceptions. Not far off the time u understand Inglors script then you could probally do it on your own with no help.

This on the other hand is a simple alternative. A simple alternative more n00b friendly, so to me this tutorial perhaps has its advantages over inglors.

Also dont forgot, while the "experienced" coders may not do it this way, this has potential to be more powerful, even more so if u want an animated hp bar, or a hp bar the changes shape as u lose hp, etc

Bair this method in mind, if you hadnt thought of it, there may come a time wen its exactly what u need and u will be so grateful ^^

Bleiz
Bleiz
  • Member since: Nov. 12, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:49:13 Reply

At 6/13/06 08:35 PM, -shmelo- wrote: Nice tutorial, but I had started a game a few weeks ago and all development came to a stop, I had finished it all up basically, and I had the health bar working the enemies were all perfect and all that.. BUT!.. when the health bar reached 0 it would just go back to full, I had tried numerous codes on trying to get it to go to the dead fraem but nothing was working. I swear I moved little bits like certain letters so then it would automatically go to the dead frame as soon as you began to play then I would replace the little letter of code or reverse it and it would do the same thing. Its just not working and whats a game if you can't die?

Well, thats easy as pai. I have NO idea why its going back to 100 if youre just adding more frames. But you could add on the frame;

onEnterFrame=function(){
if(_root.health._currentframe==1){
gotoAndStop(deadframelol);
}
}

or something, to nullify the acts of the enemy hittests.

And Welsh, thanks! ;)

pixelz
pixelz
  • Member since: Feb. 19, 2006
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:51:58 Reply

At 6/13/06 08:49 PM, darc_fayer_bleiz wrote:
At 6/13/06 08:35 PM, -shmelo- wrote: stuff

Thanks, but I think I'm gonna make a different type of health, for the game that will be easier to use frames. But that will have me going through alot of the previous code but o well. Thanks for the help : )


Whoa its me!

Bleiz
Bleiz
  • Member since: Nov. 12, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Another Way About Healthbars 2006-06-13 20:52:48 Reply

At 6/13/06 08:51 PM, -shmelo- wrote: Thanks, but I think I'm gonna make a different type of health, for the game that will be easier to use frames. But that will have me going through alot of the previous code but o well. Thanks for the help : )

NP. ^^ But i seriously dont get it, its resetting back to the frame in which its health is 100 after its reached 0? Thats got to be something that you edited in the code.

DanWandin
DanWandin
  • Member since: Jan. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Another Way About Healthbars 2006-11-18 14:05:26 Reply

Great tutorial man,

It explained alot of things to me that I didnt know, which I needed.

Thanks,

//Dan


Anal Nerve endings enable individuals to distinguish between flatus and feces, although loose stool can confuse the individual, occasionally resulting in accidental defecation

mayoarm11
mayoarm11
  • Member since: Jun. 13, 2007
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Another Way About Healthbars 2007-11-07 04:01:46 Reply

can someone please help me with a problem...
I have two movie clips, one of my main character, who walks with the arrow keys, and one of a stamina bar (with the width tweened accordingly). This is the actionscript I have for the movie clips. What I want to know is why doesn't my stamina bar shrink as my character walks.

stamina bar
onClipEvent(load){
this._width=200;
}
onClipEvent(enterFrame){
this._width=_root.stamina*2;
}

main character dude
onClipEvent(load){
_global.stamina = 100;
}
onClipEvent(enterFrame){
if (Key.isDown(Key.LEFT)){
this._x-=speed;
stamina--;
}
if (Key.isDown(Key.RIGHT)){
this._x+=speed;
stamina--;
}
if (Key.isDown(Key.UP)){
this._y-=speed;
stamina--;
}
if (Key.isDown(Key.DOWN)){
this._y+=speed;
stamina--;
}
}

btw... I already posted this in another thread, no one helped me, and that is why im posting it again here.


Awesome turret game in the making, featuring 85 uniqe turrets: News post

BBS Signature
ark99999
ark99999
  • Member since: Jul. 24, 2008
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Another Way About Healthbars 2008-08-12 03:15:22 Reply

unless u want a bar for u health, i know a much simpler health meater in numbers. i find its a LOT simpler and easer to put in other projects than eather tut i didnt make it i just find it. ENJOY!!!
http://www.flashkit.com/tutorials/Games/
Health_f-CorkySur-1129/index.php


hey i know your reading this! ya you, you know im talking about u here. its about you!
its the truth!

BBS Signature
Chandelier
Chandelier
  • Member since: Jan. 1, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Another Way About Healthbars 2008-08-12 03:43:00 Reply

At 6/13/06 08:24 PM, johnfn wrote: My healthbars use just 3 lines of code.

onClipEvent(enterFrame){
this.healthbar._xscale = this.health/this.maxhealth;
}

Every static bar user should use the above codes. It's easy to understand, take least effort to implement, and is most effective.

If beginners go around learning and using the manual methods, they'll instinctly use the inferior method when they need to perform a similar job.

Beginners should start with real solid codes. A good building has a good foundation.


BBS Signature
LordAeroEmporer
LordAeroEmporer
  • Member since: Jan. 7, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Another Way About Healthbars 2009-02-10 23:26:53 Reply

At 12/25/05 01:28 PM, Blaze wrote:
At 12/25/05 01:24 PM, Inglor wrote: now you realise that wastes a 100 frames, a mathed tween and a lot of other things, right?
I know it does Inglor, please dont kill me. :) Its just i couldnt understand your method way back, i can now. But ill still stick with this.

assuming it's on the stage root that is.
of course.

wouldn't the enterFrame do that automatically anyway in the enterFrame? oh, well, you're using gotoAndPlay and _currentframe instead of a hp or health variable,had you used one it would have been better memory-wise and would produce easier manipulation. a health=100 and then setting it to health would be cool, but you also forgot the entire linear side of it, in this tutorial health HAS to be maxed at 100 and mined at 0 for it to work
Well, i did set a variable. But I dont know what you mean by the last part of it.

you just did it reversed, variables are easier to process, are not read only and consume less CPU than accessing a movieclip's read only property and calling a function
oooh. Ok Inglor, ill remember that.

(like the hurt face in "doom")
Actually, you can still do that. Just check for numbers between the health variable, and make it like "gotoAndStop" -ish.

Well thanks Inglor, just dont kill me. Im a n00b. :)

heheh... i do something like this too. Fooled my friends who know nothing about computers other than myspace.
1) Create a health bar
2) Motion Tween on Frame 100 and transform width to 0.1
3)Make Keyframe on every 10nth (10,20,30) Frame (put "stop();")
4)put as in:
onClipEvent(enterFrame){
if(_root.enemy.hitTest(this)){
_root.vCam(or hud or whatever(i made a platformer)).hpBar.gotoAndPlay(nextFrame ());
}
}
5)well thats about it, just give the MC's their instances.
6)Remember, im an Uber Noob from Bangladesh, so take it easy!

KynetiK-27
KynetiK-27
  • Member since: Jul. 23, 2006
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Another Way About Healthbars 2009-02-11 00:16:07 Reply

I only use this 'frame' method if my health display is more complex than scaling a bar, or where scaling would warp detail. For example, what if my healthbar was a series of smaller rectangles, or a couple of heart shapes that 'cut away', or a globe of blood that would fill...

j55593
j55593
  • Member since: May. 21, 2008
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Another Way About Healthbars 2009-02-25 22:51:26 Reply

i need help with some thing (i made a platformer)
i put some spikes, and put this
onClipEvent(enterFrame){
if(this.hitTest(_root.player)){
_root.healthbar.gotoAndStop(_root.health bar._currentframe-=1);
_root.ouch.play();

}
}

ouch is a movie clip that contain a hurting sound (not important lol)
how can i make the spikes stop hurting the character after the script run once,
but it can hurt the character again after the char. left the spikes and come back into it?


Newgrounds is Wayyyyyy better than myspace or facebook

j55593
j55593
  • Member since: May. 21, 2008
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Another Way About Healthbars 2009-02-25 22:57:01 Reply

At 6/13/06 08:35 PM, pixelz wrote: ...when the health bar reached 0 it would just go back to full.

i know whats it! its because you put gotoAndStop(100); in the 1st frame, which is the 0 HP
so, you get a refill every time you ALMOST die (lol)

Its just not working and whats a game if you can't die?

yea, it kinda sucks if you can't die


Newgrounds is Wayyyyyy better than myspace or facebook

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Another Way About Healthbars 2009-02-25 23:18:19 Reply

At 2/25/09 10:51 PM, j55593 wrote: but it can hurt the character again after the char. left the spikes and come back into it?

Yes (Next time, please make another thread for this type of thing, but I'll help you out)

You need the almighty Boolean statement, which you can think of as a toggle to turn things on and off.

So you can do:

canBeHit = true;
if(canBeHit == true){ //or just if(canBeHit)
//damage here
canBeHit = false;
}

And that will only allow him to get hit once, until you turn canBeHit back to true (usually through a timer, or something like:

Counter++;
if(Counter > 50){
//reset here
Counter = 0;
}


Perpetually looking for time to return to the arts.

BBS Signature
j55593
j55593
  • Member since: May. 21, 2008
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Another Way About Healthbars 2009-02-26 21:49:16 Reply

thank you very much


Newgrounds is Wayyyyyy better than myspace or facebook

rendibsivad
rendibsivad
  • Member since: Apr. 7, 2011
  • Offline.
Forum Stats
Member
Level 05
Artist
Response to As: Another Way About Healthbars 2011-04-25 15:15:04 Reply

What about hearts? Like if you get hit you get knocked back just to regenerate (or to be invincible for a few seconds.) That kind of hittest.


the lord of our savior. el christio

BBS Signature