gpt4 book ai didi

c# - Sprite 位置比视口(viewport)移动得更快

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:08 24 4
gpt4 key购买 nike

我昨天在 XNA 中用视口(viewport)做了一些事情,但无法弄清楚为什么我使用的 Sprite 在更改位置时比我的视口(viewport)移动得更快。我觉得它可能与不同的值类型(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);
}

最佳答案

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

无论如何,它之所以这样移动是因为 SpriteBatch 的坐标系在视口(viewport)方面的客户空间

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

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

关于c# - Sprite 位置比视口(viewport)移动得更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com