2015-06-05 166 views
0

我對自己的健康有一點問題。當我移動我的人物時,健康欄從屏幕上消失。 我該如何讓它站在我的屏幕上,有些東西不喜歡移動到任何地方。但是當我移動我的人物時,它也會移動。XNA的健康吧

這裏我的代碼:

private Texture2D container, lifebar; 
    public Vector2 position; 
    public int fullHealth; 
    public int currentHealth; 
    public Healthbar(ContentManager content) 
    { 
     LoadContent(content); 
     fullHealth = lifebar.Width; 
     currentHealth = fullHealth; 
    } 

    private void LoadContent(ContentManager content) 
    { 
     container = content.Load<Texture2D>("Untitled"); 
     lifebar = content.Load<Texture2D>("Health 2"); 

    } 

    public void Update() 
    { 
     if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
      position.X += 3; 
     if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
      position.X -= 3; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(container, position ,Color.Red); 
     spriteBatch.Draw(lifebar, position, new Rectangle((int)position.X, (int)position.Y, currentHealth, lifebar.Height), Color.Pink); 
    } 

回答

0

您需要與屏幕邊界檢查position.xposition.y價值觀,否則,你將永遠失去健康吧。你想要的東西,如:

if (position.X <= 10) 
    position.X = 10; 

if (position.X >= 1270) 
    position.X = 1270;