Guest User

healthbar C# noob

a guest
Apr 14th, 2012
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* I'm receiving 3 errors:
  2.  
  3. The best overload method match for Rectangle.Rectangle(int, int, int, int) has some invalid arguments.
  4.  
  5. Argument 1: cannot convert from 'float to int'
  6. Argument 2: cannot convert from 'float to int'
  7.  
  8. */
  9.  
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Content;
  17. using Microsoft.Xna.Framework.Graphics;
  18. using Microsoft.Xna.Framework.Input;
  19.  
  20.  
  21. namespace Pong
  22. {
  23.         class HUD
  24.     {
  25.         SpriteBatch spriteBatch;
  26.         Texture2D mHealthBar;
  27.         int mCurrentHealth = 100;
  28.         private Rectangle titleSafeRectangle;
  29.         private Vector2 p1HUDLocation;
  30.         private Vector2 p2HUDLocation;
  31.  
  32.  
  33.         public void UpdateHealth(int health)
  34.         {
  35.             //TODO: probably need to do some validation on the value here
  36.             mCurrentHealth = health;
  37.         }
  38.  
  39.         public void LoadContent(ContentManager content)
  40.         {
  41.  
  42.             // Create a new SpriteBatch, which can be used to draw textures.  
  43.             spriteBatch = new SpriteBatch(Game1.Instance.GraphicsDevice); //this.  
  44.  
  45.             //Load the HealthBar image from the disk into the Texture2D object  
  46.             mHealthBar = content.Load<Texture2D>(@"gfx/healthBar2");
  47.  
  48.             // Used in the Draw method  
  49.             titleSafeRectangle = new Rectangle
  50.                 (Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
  51.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
  52.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
  53.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);
  54.  
  55.              titleSafeRectangle = new Rectangle(0, 0, 1280, 720);
  56.             //We want to draw Player 1's HUD at 25% of the screen and Player 2's HUD at 75% of the screen
  57.             //We only want to draw within the title safe rectangle area, so we can use titleSafeRect.Width.
  58.  
  59.             //With this info, we can get the location for Player 1's HUD
  60.              p1HUDLocation = new Vector2(titleSafeRectangle.Left + titleSafeRectangle.Width * 0.25f, 45);
  61.             //X position (1/4 of the way from the left side of the title safe area.
  62.  
  63.             //And player 2's HUD:
  64.              p2HUDLocation = new Vector2(titleSafeRectangle.Right - titleSafeRectangle.Width * 0.25f, 45);
  65.             //X position (1/4 of the way from the right side of the title safe area.
  66.        
  67.         }
  68.  
  69.  
  70.    
  71.  
  72.         public void Draw(GameTime gameTime)
  73.         {
  74.  
  75.  
  76.            
  77.             // Player 1 health
  78.             spriteBatch.Begin();
  79.            
  80.             //Draw the negative space for the health bar  
  81.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  82.                                     new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  83.  
  84.             //Draw the current health level based on the current Health  
  85.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30,
  86.                 (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
  87.                 new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
  88.  
  89.  
  90.             //Draw the box around the health bar  
  91.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  92.                 new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
  93.  
  94.             spriteBatch.End();
  95.  
  96.            
  97.             // Player 2 health
  98.             spriteBatch.Begin();
  99.  
  100.  
  101. // JUST COMMENTED THIS OUT FOR NOW
  102.             //Draw the negative space for the health bar  
  103.         //    spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  104.          //                           new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  105.  
  106.  
  107. // THE ERROR IS OCCURRING ON THE LINE BELOW.
  108.             spriteBatch.Draw(mHealthBar, new Rectangle(p2HUDLocation.X - mHealthBar.Width / 2, p2HUDLocation.Y, mHealthBar.Width, 44),
  109.                                      new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  110.  
  111.             //Draw the current health level based on the current Health  
  112.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30,
  113.                 (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
  114.                 new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
  115.  
  116.  
  117.             //Draw the box around the health bar  
  118.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X * 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  119.                 new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
  120.  
  121.             spriteBatch.End();
  122.         }
  123.     }
  124. }
RAW Paste Data