0

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

有什么想法可能导致这种情况吗?

4

1 回答 1

2

好消息,看起来没什么大不了的!我一直鼓励新 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 类,并创建其他继承自它的类。

于 2013-04-26T21:04:32.037 回答