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?
Thanks In Advance.
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
public void draw(Graphics g)
{
float healthScale = health / maxHealth;
g.setColor(healthBarColor);
g.fillRect(healthBarX, healthBarY, healthBarWidth * healthScale, healthBarHeight);
}
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.