Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (152) games submitted by our members Games in WIP (607) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Fighting Game Health Bars  (Read 6366 times) 0 Members and 1 Guest are viewing this topic.
vection

Senior Newbie

 « Posted 2010-06-08 22:23:26 »

Hey, I'm trying to make two health bars for my fighting game, I need to make it so when either get hit the one that gets hit the health goes down. Anyone point me in the right direction or help me out here?
Wildern

Junior Devvie

 « Reply #1 - Posted 2010-06-09 01:39:07 »

I would draw a filled rectangle for each one, with one dimension (width or height) being related to the current health of that player.
ie: if max health is 100, and player one is at 100 health, you would draw/fill a rectangle 5 pixels high and 100 pixels wide.  If player one's health is at 75, you would only draw a rectangle 75 pixels wide.

Make sense?

If max health is variable and you only have a fixed space to display the bar, you will need to do a little math to calculate the ratio.
Max health bar width divided by max health multiplied by current health to get the width of the health bar.
ie: 250 current health, 500 max health, 100 pixels max health bar width
100/500 = 0.2
0.2 * 250 = 50 pixels wide for current health
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #2 - Posted 2010-06-09 02:16:14 »

 1  2  3  4  5  6 `public void draw(Graphics g){    float healthScale = health / maxHealth;    g.setColor(healthBarColor);    g.fillRect(healthBarX, healthBarY, healthBarWidth * healthScale, healthBarHeight);}`

See my work:
OTC Software
vection

Senior Newbie

 « Reply #3 - Posted 2010-06-09 21:39:05 »

So I would have to draw/paint it every time they get hit? or can I get it so it updates all the time and when one hits the other the health goes down alone?
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #4 - Posted 2010-06-09 21:56:36 »

So I would have to draw/paint it every time they get hit? or can I get it so it updates all the time and when one hits the other the health goes down alone?
Unless you're dealing with dirty rectangles (which I'm guessing you're not), every time you are calling repaint() it erases everything and then redraws. So you'll just put the above in your draw call and have it pull stats from your characters in order to be up to date. So it'll update every frame, part of that being that it will calculate how much it should draw every frame.

See my work:
OTC Software
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (10 views) 2015-04-01 18:27:05 ags1 (27 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (48 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (33 views) 2015-03-13 17:50:51 Olo (40 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (61 views) 2015-03-10 11:36:42
 BurntPizza 21x LiquidNitrogen 21x KevinWorkman 18x EgonOlsen 17x theagentd 16x basil_ 16x Roquen 15x Varkas 12x wessles 11x 65K 11x Riven 9x Rayvolution 9x phu004 8x SHC 8x princec 8x Ashedragon 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org