我的37 * 37分辨率精灵的动画正在膨胀,因为当我按下左右箭头键时,两个动画出现在彼此的顶部。 这是我的代码:
keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.Right))
{
position = new Vector2(position.X + 1.4f, position.Y);
isRight = true;
isLeft = false;
if (keys.IsKeyDown(Keys.Left))
{
isLeft = true;
isRight = false;
}
}
if (keys.IsKeyDown(Keys.Left))
{
position = new Vector2(position.X - 1.4f, position.Y);
isLeft = true;
isRight = false;
if (keys.IsKeyDown(Keys.Right))
{
isRight = true;
isLeft = false;
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
int frame = (int)((gameTime.TotalGameTime.TotalSeconds * 12) % 4);
if (keys.IsKeyDown(Keys.Right))
spriteBatch.Draw(texture, position, new Rectangle(frame * 37, 0, 37, 37), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
if (keys.IsKeyDown(Keys.Left))
spriteBatch.Draw(texture, position, new Rectangle(frame * 37, 0, 37, 37), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 1.0f);
if (keys.IsKeyUp(Keys.Left) && keys.IsKeyUp(Keys.Right))
{
if (isRight)
spriteBatch.Draw(texture, position, new Rectangle(0, 0, 37, 37), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
if (isLeft)
spriteBatch.Draw(texture, position, new Rectangle(0, 0, 37, 37), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 1.0f);
}
spriteBatch.End();
我做错了什么?
答案 0 :(得分:0)
您有两个动画相互重叠,因为您的SpriteBatch.Draw
方法中有两个单独的Draw
个调用。当您同时按下左右键时,IsKeyDown
ifs都为真,并且两个绘图都被执行。
此外,这篇文章更适合https://gamedev.stackexchange.com/。