严重的运动滞后 - C#,XNA

时间:2013-04-26 20:19:51

标签: c# xna sprite game-physics

我是编程C#的新手,还有编程任何东西。这是我的第三个2D游戏,我遇到了一个问题。出于某种原因,当我试图移动我的精灵时,游戏变得非常快,我无法弄清楚原因。

这是羊班:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IdeaLess
{
class Sheep
{
    //-//-//-//-//-//
    public Texture2D SheepTexture;
    public const float SheepSpeed = 0.5f;
    public Vector2 SheepPosition;
    public int Width
    {
        get { return SheepTexture.Width; }
    }
    public int Height
    {
        get { return SheepTexture.Height; }
    }
    //-//-//-//-//-//
    public void Initialize(Texture2D texture, Vector2 position)
    {
        SheepTexture = texture;
        SheepPosition = position;
    }

    public void Update()
    {
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(SheepTexture, SheepPosition, null, Color.White, 0f,      Vector2.Zero, 1f, SpriteEffects.None, 0f);
    }
}
}

Game1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace IdeaLess
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    //CLASS OBJECTS
    Sheep sheep;
    Pig pig;

    //SHEEP
    float SheepMoveSpeed;
    //PIG
    float PigMoveSpeed;

    //KEYBOARDSTATES
    KeyboardState currentKeyboardState;
    KeyboardState previousKeyboardState;

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

    protected override void Initialize()
    {
        sheep = new Sheep();
        pig = new Pig();
        SheepMoveSpeed = 5f;
        PigMoveSpeed = 4f;
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Vector2 sheepPosition = new Vector2(20, 30);
        Vector2 pigPosition = new Vector2(20, 400);
        sheep.Initialize(Content.Load<Texture2D>("Sheep"), sheepPosition);
        pig.Initialize(Content.Load<Texture2D>("Pig"), pigPosition);
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        previousKeyboardState = currentKeyboardState;
        currentKeyboardState = Keyboard.GetState();
        UpdateSheep(gameTime);
        UpdatePig(gameTime);

        base.Update(gameTime);
    }
    private void UpdateSheep(GameTime gameTime)
    {
        if (currentKeyboardState.IsKeyDown(Keys.Left))
            sheep.SheepPosition.X -= SheepMoveSpeed;
        if (currentKeyboardState.IsKeyDown(Keys.Right))
            sheep.SheepPosition.X += SheepMoveSpeed;
    }
    private void UpdatePig(GameTime gameTime)
    {
        if (currentKeyboardState.IsKeyDown(Keys.A))
            pig.PigPosition.X -= PigMoveSpeed;
        if (currentKeyboardState.IsKeyDown(Keys.D))
            pig.PigPosition.X += PigMoveSpeed;
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);

        spriteBatch.Begin();

        sheep.Draw(spriteBatch);
        pig.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

到目前为止,游戏仅由玩家类(Sheep)和Game1.cs组成。还有一个猪类;和羊一样。

基本上,每当我按住'向右箭头'时,精灵就会变得不稳定且不均匀,有时会减慢到几乎静止不动,有时它会在再次滞后之前正常移动一会儿。

这不仅仅是精灵运动,而是FPS。我知道这是因为我在之前的游戏中遇到了这种滞后现象,导致背景音乐停止,定时器停止播放。

任何可能导致此问题的想法?

1 个答案:

答案 0 :(得分:2)

好消息,看起来好像没什么大不了的!我总是鼓励新的XNA程序员做的事情是添加经过时间!基本上,根据系统在给定时间的运行速度,可能会影响精灵的移动速度,具体取决于您每秒的帧数。如果你在另一台计算机上试过它,它可能会以完全不同的速度运行。

要更正此问题,您需要修改UpdateAnimal()方法

private void UpdateSheep(GameTime gameTime)
{
        //How much time has passed since the last frame, incase we lag and skip a frame, or take too long, we can process accordingly
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (currentKeyboardState.IsKeyDown(Keys.Left))
            sheep.SheepPosition.X -= SheepMoveSpeed * elapsed; // Multiply by elapsed!
        if (currentKeyboardState.IsKeyDown(Keys.Right))
            sheep.SheepPosition.X += SheepMoveSpeed * elapsed;
}

现在,根据您的计算机规格,elapsed将只是1(秒)的一小部分,因此您需要增加SheepMoveSpeed,直到您的精灵开始移动。

如果这不起作用,您可以尝试使用分析器查看导致滞后的原因,或添加fps计时器以查看它是否真的“滞后”或只是不是移动得当。

我还鼓励您创建一个Animal类,并创建从其继承的其他类。