我想知道如何创建HUD。 我目前拥有在设定坐标处绘制到屏幕的健康,法力和经验栏。这样做的缺点是当相机平移条形时保持其设定坐标,我希望它们可以调整到视口或不受位置影响,只是简单地绘制到屏幕上。
编辑 我设法使用相机的x和y坐标调整HUD。 我已经为绘制HUD创建了一个单独的类,但现在它们没有调整。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameOne.Components;
namespace GameOne.GameScreens
{
public class HUD : BaseGameState
{
Player player;
Texture2D HealthBar;
Texture2D HealthBarPositive;
Texture2D HealthBarNegative;
Texture2D ManaBar;
Texture2D ManaBarPositive;
Texture2D ManaBarNegative;
Texture2D ExpBar;
Texture2D ExpBarPositive;
int CurrentHealth = 100;
int CurrentMana = 45;
int CurrentExp = 0;
public HUD(Game game, GameStateManager manager)
: base(game, manager)
{
player = new Player(game);
}
public void LoadContent()
{
base.LoadContent();
HealthBar = Game.Content.Load<Texture2D>(@"GUI\healthBar");
HealthBarPositive = Game.Content.Load<Texture2D>(@"GUI\healthBarPositive");
HealthBarNegative = Game.Content.Load<Texture2D>(@"GUI\healthBarNegative");
ManaBar = Game.Content.Load<Texture2D>(@"GUI\manaBar");
ManaBarPositive = Game.Content.Load<Texture2D>(@"GUI\manaBarPositive");
ManaBarNegative = Game.Content.Load<Texture2D>(@"GUI\manaBarNegative");
ExpBar = Game.Content.Load<Texture2D>(@"GUI\expBar");
ExpBarPositive = Game.Content.Load<Texture2D>(@"GUI\expBarPositive");
}
public void Update(GameTime gameTime)
{
if (InputHandler.KeyDown(Keys.F1))
{
CurrentHealth += 1;
}
if (InputHandler.KeyDown(Keys.F2))
{
CurrentHealth -= 1;
}
if (InputHandler.KeyDown(Keys.F3))
{
CurrentMana += 1;
}
if (InputHandler.KeyDown(Keys.F4))
{
CurrentMana -= 1;
}
if (InputHandler.KeyDown(Keys.F5))
{
CurrentExp += 1;
}
if (InputHandler.KeyDown(Keys.F6))
{
CurrentExp -= 1;
}
CurrentHealth = (int)MathHelper.Clamp(CurrentHealth, 0, 100);
CurrentMana = (int)MathHelper.Clamp(CurrentMana, 0, 45);
CurrentExp = (int)MathHelper.Clamp(CurrentExp, 0, 500);
}
public void Draw(GameTime gameTime)
{
GameRef.SpriteBatch.Draw(
HealthBarNegative,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
HealthBarPositive,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150 * (int)CurrentHealth / 100, 15),
Color.White);
GameRef.SpriteBatch.Draw(
HealthBar,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBarNegative,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBarPositive,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150 * (int)CurrentMana / 45, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBar,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ExpBarPositive,
new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260 * (int)CurrentExp / 500, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ExpBar,
new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260, 15),
Color.White);
}
}
}
答案 0 :(得分:5)
当您绘制健康条时,它是否在您指定相机矩阵的Spritebatch.Begin(...)中?
如果你在自己的Spritebatch.Begin中绘制它,没有相机,健康栏的位置将保持相对于屏幕。
答案 1 :(得分:0)
使用DrawableGameComponent扩展类时,可以使用属性DrawOrder将其设置为最顶层。 这个类会有点不同,你必须重载Update,draw和LoadContent,但它们都是一样的。
只有在HUD与SpriteBatch一起使用时,这才有用。