【问题标题】:XNA 3.1 Laggy movementXNA 3.1 滞后运动
【发布时间】:2023-03-12 16:35:02
【问题描述】:

我有 Windows XP SP3。

我在 3.1 版本中创建了默认的 XNA 项目,并将这些简单的行添加到预生成的代码中:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;

    SpriteBatch spriteBatch;
    Rectangle rectangle = new Rectangle(80, 80, 100, 100);  
    Texture2D textureTrain;     

    public Game1()
    {            
        graphics = new GraphicsDeviceManager(this);     
        Content.RootDirectory = "Content";

        //TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 10);
    }
...

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);      

        // TODO: use this.Content to load your game content here
        textureTrain = Content.Load<Texture2D>("MyBitmap1");
    }
...

    /// <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();

        // TODO: Add your update logic here
        rectangle.X = rectangle.X + 1;
        rectangle.Y = rectangle.Y + 1;

        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.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();        
        spriteBatch.Draw(textureTrain, rectangle, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

就这么简单!但是动作很迟钝,忽闪忽闪,我都不敢看。 如果我将它与一些 Flash 游戏进行比较,它是无与伦比的。为什么是这样?我做错了什么?

【问题讨论】:

  • 请注意,XNA 4.0 已经发布了一段时间,您现在应该使用它。
  • 我测试了您的代码(尽管是在 XNA 4 中的 Vista 上),没有任何闪烁、卡顿或延迟(我也不希望在 XP 和 XNA 3.1 上出现) )。您是否能够正常畅玩 GPU 加速游戏且无延迟?有没有你没有发布的代码?
  • 那么您确定在 XP 和 vista 及更高版本上运行 XNA 之间没有区别吗?我从来没有在这台电脑上玩过游戏。是的,它是发布的完整代码。难道是我错过了一些访问 gpu 的驱动程序/库?反正这么简单的例子一定是cpu能顺利模拟出来的,还是我错了?

标签: xna game-physics


【解决方案1】:

因为您使用的是 Rectangle 的 X 和 Y 分量,所以您的计算将四舍五入到最接近的整数。在这种情况下,您需要精细、精确的动作。

Vector2 position = new Vector2(0.1f, 0.1f);
//Some Arbitrary movement
float speed = 0.008f;
position.X += (float)((speed * position.X);
position.Y += (float)((speed * position.Y);

spriteBatch.Begin();
spriteBatch.Draw(textureTrain, position, rectangle, Color.White);
spriteBatch.End();

【讨论】:

  • 当人们提出这个建议时,我很恼火,因为默认情况下,XNA 通过跳过 draw 方法来补偿延迟。除非您以固定的时间步长运行游戏,否则您真的不需要这样做。
  • 我改变了我的答案,使它更正确,这是你所期望的@Ruirize?
  • IsFixedTimeStep 是什么?会不会影响动作的流畅度?
  • 感谢您更改它。这些天我看到很多人把旧技巧误认为是 XNA 自动修复的东西。 IsFixedTimeStep 基本上控制跳帧。如果滞后,您的游戏会丢帧。将此设置为 true 会禁用此行为。但它不应该导致闪烁。
  • 你有什么推荐的? 2d 游戏的固定时间步长还是非固定时间步长?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
相关资源
最近更新 更多