Forum Topic: Health Bar Help

(128 views • 11 replies)

This topic is 1 page long.

<< < > >>
None

greyheaven

Reply To Post Reply & Quote

Posted at: 5/3/09 02:43 PM

greyheaven LIGHT LEVEL 03

Sign-Up: 09/28/01

Posts: 18

Hi guys,

I am making a simple little game in flash and I would like a health bar to go in it, what I need is a bar which I can control as required frame by frame on a + or - basis - so one frame could contain 'you get hit by a brick, lose 10 health' or another could contain 'you drink a health potion, gain 20 health'. I also need some way of checking the health bar so that when it reaches 0 the player is directed to a certain frame.

I have checked out a bunch of tutorials online but they have all been based on a collision system to reduce health, but my game will be text based. Being a total novice, I haven't been able to translate these tutorials into what I need.

I am okay with the visual side of things (making the bar and splitting the stroke into a separate layer etc.) but if anyone could help me with the code I would be very grateful!

Best,
Ben


None

4urentertainment

Reply To Post Reply & Quote

Posted at: 5/3/09 02:46 PM

4urentertainment NEUTRAL LEVEL 13

Sign-Up: 08/01/08

Posts: 1,724

The simplest method you could use that has as little AS as possible, is to keep all the frames in your health bar movieclip, and just whenever anything happens that will make u get hurt, tell the _root.health.gotoAndStop(#):

replace # with frame number

"When you look at yourself from a universal standpoint, something inside always reminds or informs you that there are bigger and better things to worry about. "- Einstein


None

Neo-13

Reply To Post Reply & Quote

Posted at: 5/3/09 02:48 PM

Neo-13 LIGHT LEVEL 21

Sign-Up: 06/09/07

Posts: 1,488

At 5/3/09 02:43 PM, greyheaven wrote: Hi guys,

I am making a simple little game in flash and I would like a health bar to go in it, what I need is a bar which I can control as required frame by frame on a + or - basis - so one frame could contain 'you get hit by a brick, lose 10 health' or another could contain 'you drink a health potion, gain 20 health'. I also need some way of checking the health bar so that when it reaches 0 the player is directed to a certain frame.

I have checked out a bunch of tutorials online but they have all been based on a collision system to reduce health, but my game will be text based. Being a total novice, I haven't been able to translate these tutorials into what I need.

I am okay with the visual side of things (making the bar and splitting the stroke into a separate layer etc.) but if anyone could help me with the code I would be very grateful!

Best,
Ben

I'm quite sure what you mean - do you want a bar that displays health, or just text...?

Anyway, if you want a health bar, draw it, and then simply adjust its _xscale property to reflect the health.

e.g. healthBar._xscale = health;

For running out of health:

If (health <= 0)
{
   gotoAndStop(frame); // or whatever
}
BBS Signature

None

greyheaven

Reply To Post Reply & Quote

Posted at: 5/3/09 02:50 PM

greyheaven LIGHT LEVEL 03

Sign-Up: 09/28/01

Posts: 18

Hi 4urentertainment,

Thanks for your reply! As far as I realise, I need to have the bar working a bit more dynamicly than that, because my game will comprise of a lot of unique frames on a different layer which will tell the 'story' as such - if that makes sense!

Cheers


None

greyheaven

Reply To Post Reply & Quote

Posted at: 5/3/09 02:53 PM

greyheaven LIGHT LEVEL 03

Sign-Up: 09/28/01

Posts: 18

Hi Neo-13,

Thanks for your help, I do want a visual health bar, with no text. I have used Xscale to display the value of my health variable but I am having trouble adjusting the health as I go along, the bar doesn't seem to move so assuming I coded the bar correctly the value of my 'health' is being lost somewhere :/


None

Neo-13

Reply To Post Reply & Quote

Posted at: 5/3/09 02:58 PM

Neo-13 LIGHT LEVEL 21

Sign-Up: 06/09/07

Posts: 1,488

You probaly aren't checking whether the health has changed. In other words, you're probably only telling the xscale of the bar to equal the health at the beginning, but when the health changes, the code needs to be executed again. Therefore, you will need to execute that code every frame, using the onEnterFrame function.

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 5/3/09 02:58 PM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,744

on your enterframe, do a:

trace(health);

to make sure the variable itself is working correctly.
Once it is, then implement the above health bar.

All sites currently down. Deal with it. <3

BBS Signature

None

greyheaven

Reply To Post Reply & Quote

Posted at: 5/4/09 06:51 AM

greyheaven LIGHT LEVEL 03

Sign-Up: 09/28/01

Posts: 18

Hey guys,

Still no success, perhaps someone can help me out, I have this code on the 'energyBar' graphic:

onClipEvent(load){
var energy = 100;
var maxenergy = energy;
}

onClipEvent(enterFrame){
energyBar._xscale = (energy / maxenergy) * 100;
}

I can't tell if its working or not, also when I want to make the health go up or down on certain frames I am trying to use:

onClipEvent(load){
energy -= 10;
}

on the health bar layer and graphic, am I doing this correctly?

Thanks again, I'm sure this is very simple for you guys but as a total novice I am finding it confusing! :)

Cheers


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 5/4/09 07:22 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,736

based on that code, we're gonna need more info. where is your health variable? what happens when u trace it? how do u tell the program to lessen health?

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

greyheaven

Reply To Post Reply & Quote

Posted at: 5/4/09 07:41 AM

greyheaven LIGHT LEVEL 03

Sign-Up: 09/28/01

Posts: 18

Sorry if I am explaining badly, hopefully this picture can illustrate what I am trying to do:

My energy bar starts on frame 50, before that is a preloader on frames 1+2 and an intro, from 50 is the one you can see, with 51 and 52 being frames displaying the outcome of each choice seen on frame 50, hopefully this makes things a bit clearer!

Cheers

Health Bar Help


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 5/4/09 09:46 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,736

ok, assuming the 50 frames are already correct (going smaller) and your health is out of 100. Inside the health bar (double click on it) make a new layer. Make sure it only has one keyframe on that layer and put this in the keyframe put the following actions (as2)

onEnterFrame = function () {
gotoAndStop(_root.health/2);//change health to the variable that ur using, and take out the /2 if ur health
//and amount of frames is the same
};

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

dragonjet

Reply To Post Reply & Quote

Posted at: 5/4/09 09:50 AM

dragonjet LIGHT LEVEL 20

Sign-Up: 12/02/05

Posts: 676

guys, take the easiest way in!
what code are you giving him?
gotoAndStop?

health=80;
maxHealth=100;

healthBar._xscale=Math.floor((health/maxHealth)*100);

Sig


All times are Eastern Standard Time (GMT -5) | Current Time: 05:04 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!