Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Health Bar & XP Bar[Solved]  (Read 618 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-07-18 02:46:10 »

Hello Everyone,

My game has a health bar and an xp bar. I want to keep the health bar at a width of 150 but my health is 100. How would i make it so if the enemy shoots me it takes 10 away from my health leaving me with 90 health left. rather than making the health bar 100px wide and taking 10 away from that. because i want to be able to have health buffs in the future.

So how do i keep the health bar a static width and change the health bar size by the amount of health my enemy is dealing. Please help

Thanks
- GlennBrann
Offline Longarmx
« Reply #1 - Posted 2013-07-18 02:58:27 »

1  
2  
3  
4  
5  
6  
7  
8  
int health = 100;
int width = 150;
float ratio = width/health;
float healthWidth;

public void update(){
     healthWidth = ratio * health;
}

Offline Tinhorn

Junior Member


Medals: 1
Projects: 2



« Reply #2 - Posted 2013-07-18 03:01:46 »

You could get the percent of life left (which will be easy since you are using 100 as maximum) and set that percent of 150 to be drawn
So when the player has 90 health, .9 * 150 width of the health bar would be drawn

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GlennBrann
« Reply #3 - Posted 2013-07-18 03:08:07 »

When ever my health is over the width of the health bar it doesn't render.
for example:

the bar with is 100px.
the player health 150 health points.
i did what you said and its not rendering..
Offline Vladiedoo
« Reply #4 - Posted 2013-07-18 03:16:06 »

It would be really helpful if you could share your health bar rendering code  Wink.
Offline GlennBrann
« Reply #5 - Posted 2013-07-18 03:17:21 »

Sorry, i'm just drawing a rectangle with and the width would be the width of 100 but my health is 150.    

1  
g.fillRect(211, 6, (int)healthWidth, barHeight);
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #6 - Posted 2013-07-18 04:30:56 »

I thought you said in your first post that the width is 150 and health is 100? Tongue

I want to keep the health bar at a width of 150 but my health is 100.

Try something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
int maxHealth, currentHealth, barWidth;

maxHealth = 150;
currentHealth = /*whatever the current health of the character is*/
barWidth = 100;

float ratio = currentHealth/maxHealth;

g.fillRect(211, 6, (int)(ratio*barWidth), barHeight);

Basically, what you want is to get the ratio of the player's current health against his maximum health and multiply that to the width of the bar.

So if the player has 75 health remaining for example, the width of your bar should be:

= (currentHealth/maxHealth)*barWidth
= (75/150)*100
= 50px

Offline GlennBrann
« Reply #7 - Posted 2013-07-18 04:51:37 »

It works like a charm... Smiley thank you everyone. PS sorry for being not clear enough in the original post.
- GlennBrann
Offline Longarmx
« Reply #8 - Posted 2013-07-18 05:09:33 »

So if the player has 75 health remaining for example, the width of your bar should be:

= (currentHealth/maxHealth)*barWidth
= (75/150)*100
= 50px

Wouldn't you want to do 75/100 * 150? Because 75 health should be about 113px.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #9 - Posted 2013-07-18 05:21:14 »

Well, he clarified that his health is 150 units and the bar is 100px... so if he has 75 units of health left, which is 50% of 150 units, his health bar should be 50px, which is 50% of 100px.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!