Guest User

HUD HP offset right

a guest
Apr 14th, 2012
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The Player 1 bar is fine.
  2.  
  3. 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.
  4.  
  5. Player 2 on the other hand, I can't figure out.
  6.  
  7. It has to do with this bit of code, taken from player 1's hp bar:
  8.  
  9. titleSafeRectangle.Center.X / 2
  10.  
  11. 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.  
  12.  
  13. I'm an idiot with math. How do I offset this the same amount to the right?
  14.  
  15. */
  16.  
  17.  
  18.            
  19.  
  20.  
  21.  // Used in the Draw method  
  22.             titleSafeRectangle = new Rectangle
  23.                 (Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
  24.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
  25.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
  26.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);
  27.         }
  28.  
  29.  
  30.  
  31.         public void Draw(GameTime gameTime)
  32.         {  
  33. // Player 1 health
  34.             spriteBatch.Begin();
  35.            
  36.             //Draw the negative space for the health bar  
  37.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  38.                                     new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  39.  
  40.             //Draw the current health level based on the current Health  
  41.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30,
  42.                 (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
  43.                 new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
  44.  
  45.  
  46.             //Draw the box around the health bar  
  47.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  48.                 new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
  49.  
  50.             spriteBatch.End();
  51.  
  52.            
  53.             // Player 2 health
  54.             spriteBatch.Begin();
  55.  
  56.             //Draw the negative space for the health bar  
  57.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  58.                                     new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  59.  
  60.             //Draw the current health level based on the current Health  
  61.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30,
  62.                 (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
  63.                 new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
  64.  
  65.  
  66.             //Draw the box around the health bar  
  67.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  68.                 new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
  69.  
  70.             spriteBatch.End();
  71.         }
  72.     }
  73. }
RAW Paste Data