XNA 3.1 Laggy运动

时间:2011-07-15 08:27:08

标签: xna game-physics

我有 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游戏进行比较,它是无与伦比的。为什么是这样?我做错了什么?

1 个答案:

答案 0 :(得分:1)

因为您使用的是矩形的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();