Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The Player 1 bar is fine.
- I also have a score for each player displayed, and it is positioned correctly. (Ex. Player 1 is 1/4 of the way over, and Player 2 is 3/4 of the way over). That is how I know that the health bar is in the correct location for player 1: it is centered evenly with the score.
- Player 2 on the other hand, I can't figure out.
- It has to do with this bit of code, taken from player 1's hp bar:
- titleSafeRectangle.Center.X / 2
- If I remove the '/', then the bar is centered within the middle of the screen. Obviously if I add the '/2' it offset it to the left. Perfect.
- I'm an idiot with math. How do I offset this the same amount to the right?
- */
- // Used in the Draw method
- titleSafeRectangle = new Rectangle
- (Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
- Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
- Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
- Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);
- }
- public void Draw(GameTime gameTime)
- {
- // Player 1 health
- spriteBatch.Begin();
- //Draw the negative space for the health bar
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
- new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
- //Draw the current health level based on the current Health
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30,
- (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
- new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
- //Draw the box around the health bar
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
- new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
- spriteBatch.End();
- // Player 2 health
- spriteBatch.Begin();
- //Draw the negative space for the health bar
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
- new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
- //Draw the current health level based on the current Health
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30,
- (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
- new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
- //Draw the box around the health bar
- spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
- new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
- spriteBatch.End();
- }
- }
- }
RAW Paste Data