Drawing a Healthbar

Started by
11 comments, last by Andy474 14 years, 10 months ago
Hi guys. I am Currently Trying to Draw a health bar. I am Drawing a Sprite(Consisting of a Black Rectangle with grey Background) at given co-ordinates, The i Draw another Sprite(simply a red Rectangle) onto this sprite. This works fine. However :

//the Rectangle that holds the full bar
    Rectangle rHealth = new Rectangle(531, 200, 55, 14); 
//the Rectangle that keeps track of health level
      Rectangle rCurHealth = new Rectangle(531, 200, (55/ myTurret.MaxHealth)*myTurret.Health, 14);

spriteBatch.Draw(tHealthbarh, rHealth, rCurHealth, Color.White);

I was Aiming to Draw this By specify that only a Certain amount of the Red Rectangle Should be Drawn as the health Decreases. the Question is : "Is
spriteBatch.Draw(tHealthbarh, rHealth, rCurHealth, Color.White);
the right spriteBatch.Draw(); function to use as this doesnt seems to redraw the Rectangle
Advertisement
the second rectangle (rCurHealth) u pass to the draw method is the texture area.

So for example if u have texture for 128 width representing your health bar and u wanna draw 50% HP, by setting the rCurHealth rectangle to 50% of original size, u only use half the texture but the sprite will stretch it to the size of the first rectangle (position on the screen)

So u must resize the first rectangle (rHealth) to make the health bar look smaller.
ok, so what if i draw the tCurrHealth (The currant healthbar) with the rect
Rectangle rCurrHealth = new Rectangle(531, 200, (55/myTurret.MaxHealth) * myTurret.CurrantHealth), 14);


I simply Draw the Rectangle of the CurrantHealth and draw the Texture in here. but yet this doesnt Work. Whynot?
You might want to check out this tutorial.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Where are you creating/modifying your rectangle? In your render loop?

Try the following for now:

Rectangle rHealth = new Rectangle(531, 200, 55, 14);Rectangle rCurHealth = new Rectangle(531, 200, 55 * (myTurret.Health / myTurret.MaxHealth), 14);...SpriteBatch.Draw (tHealthbarh, rHealth, Color.White)SpriteBatch.Draw (tHealthbarh, rCurHealth, Color.White)


If that doesn't give you what you want, you may need to provide some more source code.

Try to use MSDN to see which constructors you should be using and what each variable actually relates to: SpriteBatch.Draw Method.
ok, i have looked at that Tutorial Before, I cant follow what he's doing atg all on any of his tutorials. I don't know why I just cant lol.

With

Rectangle rCurrHealth = new Rectangle(531, 200, (55/myTurret.MaxHealth) * myTurret.CurrantHealth), 14);

I now get the Desired effect.
Except. once health goes below the Max(100) in this case the sprite bar disappears completely

is this because the Rectangle requires a int Width, and I am passing a double/float?
e.g
99/100 * 55 = 54.45
The compiler/debugger should be spitting out errors if it doesn't like the variables used in the constructor.

Try casting the result of the calculation to an int and see if you have any joy.
Hey,

what types are myTurret.MaxHealth resp. myTurret.Health?

If they are ints the result of 55/myTurret.MaxHealth is 0 and you are drawing a rectangle with a width of 0px.
class Turret
{
....
Public int MaxHealth;
Public int Health;
}

private void SetupTurret
{
Turret myTurret = new Turret()
myTurret.Maxhealth = 100;
myTurret.Health = 100;
}

as I said. I am calculating the values on my calulator and they come out nicly at 54.45, 53.9 etc. but in my code any value for myTurret.Health < 100; results in no Rectangle

and i have tried
Rectangle rCurHealth = new Rectangle(531, 200, (int)(55/ myTurret.MaxHealth)*myTurret.Health, 14);


I did try Math.Round(); aswell but that didnt make a difference
yeah it'll work on your calculator. if it comes to programming the return type of a division depends on the types of the operands. if both operands are ints the return type is int as well.

so, the return value of 55/99 will be 0. whereas 55.0/99 will be 0.55556.

This topic is closed to new replies.

Advertisement