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

本文关键字:XNA 滞后 运动 | 更新日期: 2023-09-27 18:11:08

我对c#编程非常陌生,嗯,编程任何东西。这是我的第三款2D游戏,我遇到了一个问题。由于某些原因,当我试图移动精灵时,游戏变得非常慢,我不知道为什么。

Sheep类:

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

And the game .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。我知道这一点,因为我在之前的游戏中遇到过这种延迟,它导致背景音乐停止,计时器停止滴答。

你知道是什么原因导致的吗?

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

好消息,看起来没什么大不了的!我总是鼓励新XNA程序员做的一件事是添加Elapsed time!基本上,取决于你的系统在给定时间运行的速度,可能会影响你的精灵移动的速度,这取决于你每秒有多少帧。如果您在其他计算机上尝试此操作,它可能以完全不同的速度运行。

要纠正这个问题,您需要修改您的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类,并创建从它继承的其他类