C# XNA Sprite帧不一致性
我有一个爆炸精灵,有16帧 每当玩家与小行星碰撞时,都会调用lose()函数 在lose()函数中,此代码绘制动画精灵表,该精灵表在16帧动画中运行。它应该总是从第0帧开始,但并不总是这样C# XNA Sprite帧不一致性,c#,xna,sprite,C#,Xna,Sprite,我有一个爆炸精灵,有16帧 每当玩家与小行星碰撞时,都会调用lose()函数 在lose()函数中,此代码绘制动画精灵表,该精灵表在16帧动画中运行。它应该总是从第0帧开始,但并不总是这样 expFrameID = 0; expFrameID += (int)(time * expFPS) % expFrameCount; Rectangle expRect = new Rectangle(expFrameID * expFrameWidth, 0
expFrameID = 0;
expFrameID += (int)(time * expFPS) % expFrameCount;
Rectangle expRect = new Rectangle(expFrameID * expFrameWidth, 0, 63, 63);
spriteBatch.Draw(explosion, shipLocation, expRect, Color.White, 0f, new Vector2(32, 32), 1.0f, pickupSpriteEffects, 0.1f);
if (expFrameID == 15)
expIsDrawn = true;
时间变量如下所示,它获取总的游戏时间
time = (float)gameTime.TotalGameTime.TotalSeconds;
我认为lose()函数中的以下行是问题所在:
expFrameID += (int)(time * expFPS) % expFrameCount;
它可以工作,也可以制作动画。但是,它并不总是从第0帧开始。它从0到16之间的随机帧开始,但仍然以每秒16帧的速度正确运行
那么,如何使其始终从第0帧开始?在动画类中,您可能希望在draw方法中像这样增加时间变量
// This will increase time by the milliseconds between update calls.
time += (float)gameTime.ElapsedGameTime.Milliseconds;
然后检查是否有足够的时间显示下一帧
if(time >= (1f / expFPS * 1000f) //converting frames per second to milliseconds per frame
{
expFrameID++;
//set the time to 0 to start counting for the next frame
time = 0f;
}
然后从精灵表中获取要绘制的矩形
Rectangle expRect = new Rectangle(expFrameID * expFrameWidth, 0, 63, 63);
然后画精灵
spriteBatch.Draw(explosion, shipLocation, expRect, Color.White, 0f, new Vector2(32, 32), 1.0f, pickupSpriteEffects, 0.1f);
然后检查动画是否结束
if(expFrameID == 15)
{
//set expFrameID to 0
expFrameID = 0;
expIsDrawn = true;
}
改用
ElapsedGameTime
。自己在自己的变量中累积时间(或帧编号)。如何累积时间/帧编号?在类中设置一个变量,如float time
。在更新方法中,执行time+=(float)gameTime.ElapsedGameTime.TotalSeconds
。或者你可以为你在的任何画面增加一个计数器。