0

我有一个爆炸精灵,有 16 帧。

每次玩家与小行星碰撞时,都会调用 loss() 函数。

在 lost() 函数中,这段代码绘制了动画 spritesheet,它贯穿了 16 帧动画。它应该始终从第 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;

我认为问题在于 loss() 函数的以下行:

expFrameID += (int)(time * expFPS) % expFrameCount;

它工作,它动画。但是它并不总是从第 0 帧开始。它从 0 到 16 之间的随机帧开始,但它仍然以每秒 16 帧的速度正确运行。

那么,如何让它始终从第 0 帧开始呢?

4

1 回答 1

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;
}
于 2012-09-17T17:00:41.867 回答