XNA C# HUD SpriteFont

发布于 2024-12-07 18:01:19 字数 3786 浏览 1 评论 0原文

我在 HUD 中添加了 SpriteFont,这就是我得到的:

http ://i557.photobucket.com/albums/ss13/KookehMonsters/Dev/Untitled-1.png

所有这些白点来自我的相机平移和跟随的文字。 这是怎么回事?

        Label displayName = new Label();

        displayName.Text = "DisplayName";
        displayName.Size = displayName.SpriteFont.MeasureString(displayName.Text);
        displayName.Position = new Vector2((int)player.Camera.Position.X, (int)player.Camera.Position.Y);

        ControlManager.Add(displayName);

        ControlManager.Draw(GameRef.SpriteBatch);

Label.cs

public class Label : Control
{
    public Label()
    {
        tabStop = false;
    }

    public override void Update(GameTime gameTime)
    {
    }

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.DrawString(SpriteFont, Text, Position, Color);
    }

    public override void HandleInput()
    {
    }
}

GamePlayScreen.cs 的更新方法

public override void Update(GameTime gameTime)
    {
        player.Update(gameTime);
        sprite.Update(gameTime);
        hud.Update(gameTime);

        if (InputHandler.KeyReleased(Keys.Add))
        {
            player.Camera.ZoomIn();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }
        else if (InputHandler.KeyReleased(Keys.Subtract))
        {
            player.Camera.ZoomOut();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }

        Vector2 motion = new Vector2();

        if (InputHandler.KeyDown(Keys.W))
        {
            sprite.CurrentAnimation = AnimationKey.Up;
            motion.Y = -1;
        }
        else if (InputHandler.KeyDown(Keys.S))
        {
            sprite.CurrentAnimation = AnimationKey.Down;
            motion.Y = 1;
        }

        if (InputHandler.KeyDown(Keys.A))
        {
            sprite.CurrentAnimation = AnimationKey.Up;
            motion.X = -1;
        }
        else if (InputHandler.KeyDown(Keys.D))
        {
            sprite.CurrentAnimation = AnimationKey.Down;
            motion.X = 1;
        }

        if (motion != Vector2.Zero)
        {
            sprite.IsAnimating = true;
            motion.Normalize();

            sprite.Position += motion * sprite.Speed;
            sprite.LockToMap();

            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }
        else
        {
            sprite.IsAnimating = false;
        }

        if (InputHandler.KeyReleased(Keys.F))
        {
            player.Camera.ToggleCameraMode();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }

        if (player.Camera.CameraMode != CameraMode.Follow)
        {
            if (InputHandler.KeyReleased(Keys.C))
            {
                player.Camera.LockToSprite(sprite);
            }
        }

        base.Update(gameTime);
    }

GamePlayScreen.cs 的 Draw 方法

public override void Draw(GameTime gameTime)
    {
        GameRef.SpriteBatch.Begin(
            SpriteSortMode.Deferred,
            BlendState.AlphaBlend,
            SamplerState.PointClamp,
            null,
            null,
            null,
            player.Camera.Transformation);

        map.Draw(GameRef.SpriteBatch, player.Camera);

        sprite.Draw(gameTime, GameRef.SpriteBatch, player.Camera);

        hud.Draw(gameTime);

        base.Draw(gameTime);

        GameRef.SpriteBatch.End();
    }

I added a SpriteFont to my HUD and this is what I get:

http://i557.photobucket.com/albums/ss13/KookehMonsters/Dev/Untitled-1.png

All those white spots are from my camera panning and the text following along.
What's going on?

        Label displayName = new Label();

        displayName.Text = "DisplayName";
        displayName.Size = displayName.SpriteFont.MeasureString(displayName.Text);
        displayName.Position = new Vector2((int)player.Camera.Position.X, (int)player.Camera.Position.Y);

        ControlManager.Add(displayName);

        ControlManager.Draw(GameRef.SpriteBatch);

Label.cs

public class Label : Control
{
    public Label()
    {
        tabStop = false;
    }

    public override void Update(GameTime gameTime)
    {
    }

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.DrawString(SpriteFont, Text, Position, Color);
    }

    public override void HandleInput()
    {
    }
}

Update method from GamePlayScreen.cs

public override void Update(GameTime gameTime)
    {
        player.Update(gameTime);
        sprite.Update(gameTime);
        hud.Update(gameTime);

        if (InputHandler.KeyReleased(Keys.Add))
        {
            player.Camera.ZoomIn();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }
        else if (InputHandler.KeyReleased(Keys.Subtract))
        {
            player.Camera.ZoomOut();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }

        Vector2 motion = new Vector2();

        if (InputHandler.KeyDown(Keys.W))
        {
            sprite.CurrentAnimation = AnimationKey.Up;
            motion.Y = -1;
        }
        else if (InputHandler.KeyDown(Keys.S))
        {
            sprite.CurrentAnimation = AnimationKey.Down;
            motion.Y = 1;
        }

        if (InputHandler.KeyDown(Keys.A))
        {
            sprite.CurrentAnimation = AnimationKey.Up;
            motion.X = -1;
        }
        else if (InputHandler.KeyDown(Keys.D))
        {
            sprite.CurrentAnimation = AnimationKey.Down;
            motion.X = 1;
        }

        if (motion != Vector2.Zero)
        {
            sprite.IsAnimating = true;
            motion.Normalize();

            sprite.Position += motion * sprite.Speed;
            sprite.LockToMap();

            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }
        else
        {
            sprite.IsAnimating = false;
        }

        if (InputHandler.KeyReleased(Keys.F))
        {
            player.Camera.ToggleCameraMode();
            if (player.Camera.CameraMode == CameraMode.Follow)
                player.Camera.LockToSprite(sprite);
        }

        if (player.Camera.CameraMode != CameraMode.Follow)
        {
            if (InputHandler.KeyReleased(Keys.C))
            {
                player.Camera.LockToSprite(sprite);
            }
        }

        base.Update(gameTime);
    }

Draw method of GamePlayScreen.cs

public override void Draw(GameTime gameTime)
    {
        GameRef.SpriteBatch.Begin(
            SpriteSortMode.Deferred,
            BlendState.AlphaBlend,
            SamplerState.PointClamp,
            null,
            null,
            null,
            player.Camera.Transformation);

        map.Draw(GameRef.SpriteBatch, player.Camera);

        sprite.Draw(gameTime, GameRef.SpriteBatch, player.Camera);

        hud.Draw(gameTime);

        base.Draw(gameTime);

        GameRef.SpriteBatch.End();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-12-14 18:01:19

不要用相机变换来绘制 HUD,当然也不要用相机更新你的 HUD 位置,因为它们现在已经修复了。

public override void Draw(GameTime gameTime)
{
    GameRef.SpriteBatch.Begin(
        SpriteSortMode.Deferred,
        BlendState.AlphaBlend,
        SamplerState.PointClamp,
        null,
        null,
        null,
        player.Camera.Transformation);

    map.Draw(GameRef.SpriteBatch, player.Camera);

    sprite.Draw(gameTime, GameRef.SpriteBatch, player.Camera);

    base.Draw(gameTime);

    GameRef.SpriteBatch.End();

    GameRef.SpriteBatch.Begin();
    hud.Draw(gameTime);
    GameRef.SpriteBatch.End();
}

Don't draw the hud with the camera transform, and of course don't update your hud positions with the camera, because they are fixed now.

public override void Draw(GameTime gameTime)
{
    GameRef.SpriteBatch.Begin(
        SpriteSortMode.Deferred,
        BlendState.AlphaBlend,
        SamplerState.PointClamp,
        null,
        null,
        null,
        player.Camera.Transformation);

    map.Draw(GameRef.SpriteBatch, player.Camera);

    sprite.Draw(gameTime, GameRef.SpriteBatch, player.Camera);

    base.Draw(gameTime);

    GameRef.SpriteBatch.End();

    GameRef.SpriteBatch.Begin();
    hud.Draw(gameTime);
    GameRef.SpriteBatch.End();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文