Forum Topic: As: Another Way About Healthbars

(3,475 views • 20 replies)

This topic is 1 page long.

<< < > >>
None

Blaze

Reply To Post Reply & Quote

Posted at: 12/25/05 01:03 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,957

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!


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 12/25/05 01:12 PM

GuyWithHisComp LIGHT LEVEL 24

Sign-Up: 11/10/05

Posts: 4,027

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

None

Blaze

Reply To Post Reply & Quote

Posted at: 12/25/05 01:17 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,957

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.


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 12/25/05 01:21 PM

GuyWithHisComp LIGHT LEVEL 24

Sign-Up: 11/10/05

Posts: 4,027

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

BBS Signature

None

Blaze

Reply To Post Reply & Quote

Posted at: 12/25/05 01:22 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,957

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.


None

authorblues

Reply To Post Reply & Quote

Posted at: 12/25/05 01:23 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,342

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

wii friend codes: [LISTED]

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 12/25/05 01:24 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,957

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


None

Blaze

Reply To Post Reply & Quote

Posted at: 12/25/05 01:28 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,957

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


Questioning

Mad-Mardigan

Reply To Post Reply & Quote

Posted at: 6/13/06 08:14 PM

Mad-Mardigan LIGHT LEVEL 14

Sign-Up: 03/06/06

Posts: 952

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

My Flash/My Music
Members of the Sicilian, Polish and German race are better than you

BBS Signature

None

johnfn

Reply To Post Reply & Quote

Posted at: 6/13/06 08:24 PM

johnfn LIGHT LEVEL 19

Sign-Up: 08/16/03

Posts: 2,852

My healthbars use just 3 lines of code.

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


None

Bleiz

Reply To Post Reply & Quote

Posted at: 6/13/06 08:26 PM

Bleiz LIGHT LEVEL 07

Sign-Up: 11/12/05

Posts: 123

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.


None

pixelz

Reply To Post Reply & Quote

Posted at: 6/13/06 08:35 PM

pixelz DARK LEVEL 22

Sign-Up: 02/19/06

Posts: 2,943

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!


None

MARINO

Reply To Post Reply & Quote

Posted at: 6/13/06 08:42 PM

MARINO FAB LEVEL 08

Sign-Up: 02/21/06

Posts: 862

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

None

xWELSHxDRAGONx

Reply To Post Reply & Quote

Posted at: 6/13/06 08:44 PM

xWELSHxDRAGONx NEUTRAL LEVEL 07

Sign-Up: 04/13/06

Posts: 505

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


None

Bleiz

Reply To Post Reply & Quote

Posted at: 6/13/06 08:49 PM

Bleiz LIGHT LEVEL 07

Sign-Up: 11/12/05

Posts: 123

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


None

pixelz

Reply To Post Reply & Quote

Posted at: 6/13/06 08:51 PM

pixelz DARK LEVEL 22

Sign-Up: 02/19/06

Posts: 2,943

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!


None

Bleiz

Reply To Post Reply & Quote

Posted at: 6/13/06 08:52 PM

Bleiz LIGHT LEVEL 07

Sign-Up: 11/12/05

Posts: 123

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.


None

DanWandin

Reply To Post Reply & Quote

Posted at: 11/18/06 02:05 PM

DanWandin DARK LEVEL 14

Sign-Up: 01/09/05

Posts: 668

Great tutorial man,

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

Thanks,

//Dan

=]

BBS Signature

Sad

mayoarm11

Reply To Post Reply & Quote

Posted at: 11/7/07 04:01 AM

mayoarm11 NEUTRAL LEVEL 14

Sign-Up: 06/13/07

Posts: 795

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 around 80 uniqe turrets: suggestion thread | News post | Preview Image

BBS Signature

Elated

ark99999

Reply To Post Reply & Quote

Posted at: 8/12/08 03:15 AM

ark99999 EVIL LEVEL 09

Sign-Up: 07/24/08

Posts: 232

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

None

Chandelier

Reply To Post Reply & Quote

Posted at: 8/12/08 03:43 AM

Chandelier DARK LEVEL 11

Sign-Up: 01/01/08

Posts: 179

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:16 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!