2011-09-18 2 views
2

HUD를 만드는 방법에 대해 궁금해하고있었습니다. 현재 건강, 마나, 경험치 막대가 설정된 좌표로 화면에 그려져 있습니다. 이 단점은 바코드가 설정된 좌표에서 팬이 움직일 때 뷰포트에 맞추거나 위치에 영향을받지 않고 단순히 화면에 그려야한다는 것입니다.XNA C# 헤드 업 디스플레이 만들기

  • 편집 나는 카메라의 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

카메라가 움직일 때 좌표를 업데이트하는 것이 잘못된 이유는 무엇입니까? – mrduclaw

답변

5

당신이 당신의 건강 막대를 그리는, 당신은 당신의 카메라 행렬을 지정 Spritebatch.Begin (...) 내부에 그것을인가?

카메라없이 자체 Spritebatch.Begin에 그리면 헬스 바의 위치가 화면에 상대적으로 유지됩니다.

0

DrawableGameComponent로 클래스를 확장하면 DrawOrder 속성을 사용하여 클래스를 최상으로 설정할 수 있습니다. 클래스가 조금 다를 것입니다. 업데이트, 그리기 및 LoadContent를 오버로드해야하지만 모두 동일합니다.

SpriteBatch와 함께 HUD를 사용하는 경우에만 유용합니다.