XNA 3.1 迟缓运动

发布于 2024-11-24 10:06:51 字数 2247 浏览 2 评论 0原文

我有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游戏相比,那是无可比拟的。这是为什么呢?我做错了什么?

I have Windows XP SP3.

I created the default XNA project in version 3.1 and added these simple lines to the pregenerated code:

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);
    }
}

as simple as this ! But the movement is very laggy, it flickers, I can't even look at it.
If I compare it to some flash games it's incomparable. Why is this? What am I doing wrong?

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

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

发布评论

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

评论(1

素年丶 2024-12-01 10:06:51

由于您使用的是矩形的 X 和 Y 分量,因此您的计算将四舍五入到最接近的整数。在这种情况下,您需要粒度,即精细、精确的运动。

IE

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

Because your are using the X and Y components of the Rectangle, your calculation will be rounded to the nearest whole number. You want granularity in this case, fine, precise movements.

i.e.

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