精灵位置移动速度比视口快

发布于 2024-10-17 08:50:19 字数 1986 浏览 6 评论 0原文

昨天我在 XNA 中使用视口做了一些事情,并且无法弄清楚为什么我使用的精灵在更改位置时移动得比我的视口更快。我有一种感觉,它可能与不同的值类型(int 与 float)有关,但是有人愿意详细说明这一点吗?

这是我使用的代码...

    Viewport myViewport;
    Texture2D t;
    SpriteFont f;
    Vector2 point = new Vector2(0, 0);  

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        Keys[] pressedKeys = Keyboard.GetState().GetPressedKeys();
        for (int i = 0; i < pressedKeys.Length; i++)
        {
            if (pressedKeys[i] == Keys.W)
            {
                point.Y--;
            }
            else if (pressedKeys[i] == Keys.A)
            {
                point.X--;
            }
            else if (pressedKeys[i] == Keys.S)
            {
                point.Y++;
            }
            else if (pressedKeys[i] == Keys.D)
            {
                point.X++;
            }
        }
        myViewport.X = (int)point.X;
        myViewport.Y = (int)point.Y;
        GraphicsDevice.Viewport = myViewport;
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        spriteBatch.Begin();
        spriteBatch.Draw(t, new Vector2(point.X, point.Y), Color.White);
        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

I was doing some things with a viewport yesterday in XNA, and couldn't figure out why the sprite I'm using moves faster than my viewport when changing the positions. I had a feeling that it may have something to do with the different value types (int vs. float), but would someone care to elaborate on this?

Here's the code I was using...

    Viewport myViewport;
    Texture2D t;
    SpriteFont f;
    Vector2 point = new Vector2(0, 0);  

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        Keys[] pressedKeys = Keyboard.GetState().GetPressedKeys();
        for (int i = 0; i < pressedKeys.Length; i++)
        {
            if (pressedKeys[i] == Keys.W)
            {
                point.Y--;
            }
            else if (pressedKeys[i] == Keys.A)
            {
                point.X--;
            }
            else if (pressedKeys[i] == Keys.S)
            {
                point.Y++;
            }
            else if (pressedKeys[i] == Keys.D)
            {
                point.X++;
            }
        }
        myViewport.X = (int)point.X;
        myViewport.Y = (int)point.Y;
        GraphicsDevice.Viewport = myViewport;
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        spriteBatch.Begin();
        spriteBatch.Draw(t, new Vector2(point.X, point.Y), Color.White);
        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

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

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

发布评论

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

评论(1

站稳脚跟 2024-10-24 08:50:19

首先,您可能应该在 Draw 函数中设置视口。其次,您应该确保您的视口边界始终保留在屏幕上!

不管怎样,它这样移动的原因是因为 SpriteBatch 的坐标系在视口的客户端空间中

换句话说,根据 SpriteBatch,位置 (0,0) 是 GraphicsDevice.Viewport 的左上角。

这就是为什么您的精灵以预期速度两倍的速度移动,因为您实际上正在执行两个不同的操作来修改其渲染位置。

First of all, you should probably be setting the Viewport in your Draw function. Second of all you should ensure that your Viewport bounds always remain on screen!

Anyway, the reason it moves like that is because SpriteBatch's coordinate system is in client space in terms of the Viewport.

In other words, the position (0,0), according to SpriteBatch, is the top left corner of the GraphicsDevice.Viewport.

This is why your sprite moves at twice the speed you expect, because you're effectively doing two different operations modifying its rendering position.

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