您如何查看密钥是否已被释放?
我想例如,当用户释放Left键时,角色的动画停止并停留在他正在看/面向左的框架上
这是我的播放器类:
public class Player
{
#region Animation
int currentFrame;
int frameWidth;
int frameHeight;
float timer;
float interval = 65;
#endregion
private Texture2D texture;
private Vector2 position = new Vector2(64, 200);
private Vector2 velocity;
private Rectangle rectangle;
private bool isMoving;
KeyboardState keyState;
public enum playerStates
{
RIGHT,
LEFT,
WALKINGRIGHT,
WALKINGLEFT
}
playerStates currentPlayerState = playerStates.LEFT;
private bool hasJumped = false;
public Vector2 Position
{
get { return position; }
}
public Player(Texture2D newTexture, Vector2 newPosition, int newFrameWidth, int newFrameHeight)
{
texture = newTexture;
position = newPosition;
frameWidth = newFrameWidth;
frameHeight = newFrameHeight;
isMoving = false;
}
public void LoadContent(ContentManager Content)
{
texture = Content.Load<Texture2D>("Mario/full");
}
public void Update(GameTime gameTime)
{
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
position = position + velocity;
#region Key Presses
KeyboardState lastKeyState = keyState;
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Left))
{
//position.X -= 1;
AnimateLeft(gameTime);
currentPlayerState = playerStates.LEFT;
isMoving = true;
}
if (keyState.IsKeyDown(Keys.Right))
{
//position.X -= 1;
AnimateRight(gameTime);
currentPlayerState = playerStates.RIGHT;
isMoving = true;
}
//Check for last keypresses
#endregion
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1);
}
#region DrawAnimation
public void AnimateLeft(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 3 || currentFrame < 2)
{
currentFrame = 2;
}
}
}
public void AnimateRight(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 5 || currentFrame < 3)
{
currentFrame = 4;
}
}
}
#endregion
}