Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* I'm receiving 3 errors:
- The best overload method match for Rectangle.Rectangle(int, int, int, int) has some invalid arguments.
- Argument 1: cannot convert from 'float to int'
- Argument 2: cannot convert from 'float to int'
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace Pong
- {
- class HUD
- {
- SpriteBatch spriteBatch;
- Texture2D mHealthBar;
- int mCurrentHealth = 100;
- private Rectangle titleSafeRectangle;
- private Vector2 p1HUDLocation;
- private Vector2 p2HUDLocation;
- public void UpdateHealth(int health)
- {
- //TODO: probably need to do some validation on the value here
- mCurrentHealth = health;
- }
- public void LoadContent(ContentManager content)
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(Game1.Instance.GraphicsDevice); //this.
- //Load the HealthBar image from the disk into the Texture2D object
- mHealthBar = content.Load<Texture2D>(@"gfx/healthBar2");
- // 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);
- titleSafeRectangle = new Rectangle(0, 0, 1280, 720);
- //We want to draw Player 1's HUD at 25% of the screen and Player 2's HUD at 75% of the screen
- //We only want to draw within the title safe rectangle area, so we can use titleSafeRect.Width.
- //With this info, we can get the location for Player 1's HUD
- p1HUDLocation = new Vector2(titleSafeRectangle.Left + titleSafeRectangle.Width * 0.25f, 45);
- //X position (1/4 of the way from the left side of the title safe area.
- //And player 2's HUD:
- p2HUDLocation = new Vector2(titleSafeRectangle.Right - titleSafeRectangle.Width * 0.25f, 45);
- //X position (1/4 of the way from the right side of the title safe area.
- }
- 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();
- // JUST COMMENTED THIS OUT FOR NOW
- //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);
- // THE ERROR IS OCCURRING ON THE LINE BELOW.
- spriteBatch.Draw(mHealthBar, new Rectangle(p2HUDLocation.X - mHealthBar.Width / 2, p2HUDLocation.Y, 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