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