Animation getKeyFrame方法接受不断递增的浮点值。为什么?

Animation getKeyFrame方法接受不断递增的浮点值。为什么?,animation,libgdx,Animation,Libgdx,我在看一本libgdx的书。其中,作者使用动画类来播放动画。以下是相关代码: public class Flappee{ private float animationTimer = 0; private static final float FRAME_DURATION=0.25f; public Flappee(Texture texture){ TextureRegion[][] flappeeTextures = new TextureRegion(texture).sp

我在看一本libgdx的书。其中,作者使用动画类来播放动画。以下是相关代码:

public class Flappee{

private float animationTimer = 0;

private static final float FRAME_DURATION=0.25f;

public Flappee(Texture texture){

    TextureRegion[][] flappeeTextures = new TextureRegion(texture).split(118,118);
    collisionCircle = new Circle(x,y,COLLISION_RADIUS);
    this.animation = new Animation(FRAME_DURATION,flappeeTextures[0][0],flappeeTextures[0][1]);
    animation.setPlayMode(Animation.PlayMode.LOOP);
}

public void update(float delta){
        animationTimer+=delta; //called in render method of ScreenAdapter class
}


public void draw(SpriteBatch batch){
        TextureRegion texture = animation.getKeyFrame(animationTimer);
        batch.draw(//drawing commands);
    }
}

我不明白为什么每次渲染循环都需要增加animationTimer。如您所见,关键是
getKeyFrame
返回正确的纹理区域。我想这与一直循环相同的动画有关。我可以要求对此主题进行更多解释吗?

传递给渲染的增量值是自上一帧以来的时间量,因此,通过将其添加到
animationTimer
每次调用渲染,意味着
animationTimer
保留自动画开始运行以来的时间量

这需要计算出在任何给定时间显示什么帧。因此,如果您希望动画以每秒5帧的速度运行,并且
animationTimer
为400ms,则可以确定它应该显示第2帧

它不会在每次调用渲染时简单地拾取下一帧,因为这会将动画速度与帧速率联系起来,而帧速率并不总是理想的。而且framrates不是常数,因此这种方法可以确保消除任何不一致