Be a Supporter!

Health Bar Help

  • 303 Views
  • 11 Replies
New Topic Respond to this Topic
greyheaven
greyheaven
  • Member since: Sep. 28, 2001
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Health Bar Help 2009-05-03 14:43:59 Reply

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

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Health Bar Help 2009-05-03 14:46:39 Reply

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

Neo-13
Neo-13
  • Member since: Jun. 9, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to Health Bar Help 2009-05-03 14:48:39 Reply

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
greyheaven
greyheaven
  • Member since: Sep. 28, 2001
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Health Bar Help 2009-05-03 14:50:25 Reply

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

greyheaven
greyheaven
  • Member since: Sep. 28, 2001
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Health Bar Help 2009-05-03 14:53:11 Reply

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

Neo-13
Neo-13
  • Member since: Jun. 9, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to Health Bar Help 2009-05-03 14:58:11 Reply

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
Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to Health Bar Help 2009-05-03 14:58:34 Reply

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.


Perpetually looking for time to return to the arts.

BBS Signature
greyheaven
greyheaven
  • Member since: Sep. 28, 2001
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Health Bar Help 2009-05-04 06:51:56 Reply

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

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Health Bar Help 2009-05-04 07:22:42 Reply

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
greyheaven
greyheaven
  • Member since: Sep. 28, 2001
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Health Bar Help 2009-05-04 07:41:01 Reply

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

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Health Bar Help 2009-05-04 09:46:42 Reply

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
dragonjet
dragonjet
  • Member since: Dec. 2, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Health Bar Help 2009-05-04 09:50:26 Reply

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

health=80;
maxHealth=100;

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