Sign in to follow this  

Drawing a Healthbar

This topic is 3113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
[code]
Rectangle rCurrHealth = new Rectangle(531, 200, (55/myTurret.MaxHealth) * myTurret.CurrantHealth), 14);
[code]
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Andy474
so 55/100 * 90 = 50

will be 0, because 0*90 = 0


Quote:
Original post by Andy474
so if i changed my declaration of Health to a double or a float then it should work?

yes, you could either do this or simply do 55.0/myTurret.MaxHealth*myTurret.Health for the calculation of the new width.

Share this post


Link to post
Share on other sites

This topic is 3113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this