XNA的健康吧

时间:2015-06-05 13:14:39

标签: xna-4.0

我的健康状况有点麻烦。当我移动我的人物时,健康栏就跳出了屏幕。 如何让它保持在我的屏幕上,有些东西不喜欢在任何地方移动。但是,当我移动我的人物时,它也会移动。

这是我的代码:

 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); 
    }

1 个答案:

答案 0 :(得分:0)

您需要使用屏幕边界检查position.xposition.y值,否则您将始终丢失健康栏。你想要这样的东西:

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

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