【发布时间】:2023-08-10 03:52:02
【问题描述】:
我是新来的,在编码方面并不是最好的。 (为此使用 XNA 的单游戏变体) 我已经成功地重新创建了一个类来为我制作的精灵表设置动画,但我遇到的问题是,当我尝试实现按键和动画时,动画停止移动(卡在第一帧)
我的更新类看起来像这样
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();
if (previousKBState.IsKeyDown(Keys.D))
{
playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\walk_right.png"), 5, 15, 672, 631, 30);
}
else if (previousKBState.IsKeyDown(Keys.A))
{
playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\walk_left.png"), 5, 15, 672, 631, 30);
}
else
{
playerSprite.LoadGraphic(Content.Load<Texture2D>("Sprites\\idle_right.png"), 5, 10, 610, 423, 5);
}
这是我的 LoadGraphic 类
public void LoadGraphic(Texture2D texture, int rows, int columns, int width, int height, int animationSpeed)
{
this.Texture = texture;
this.rows = rows;
this.columns = columns;
this.width = width;
this.height = height;
this.animationSpeed = (float)1 / animationSpeed;
totalElapsed = 0;
currentRow = 0;
currentColumn = 0;
}
我的绘画课
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color)
{
spriteBatch.Draw(Texture,
new Rectangle((int)position.X, (int)position.Y, width, height),
new Rectangle(currentColumn * width, currentRow * height, width, height), color);
}
所以我认为我遇到的问题是,当我按住 D 或 A 键时,LoadGraphics 代码行会在动画播放之前不断刷新对另一个类的调用。
如果有人可以给我任何解决此问题的建议,将会有所帮助 谢谢
【问题讨论】:
标签: animation xna sprite monogame sprite-sheet