2015-12-10 28 views
0

我正在關注一本libgdx書。其中,作者使用Animation類來播放動畫。這是相關的代碼:getKeyFrame方法接受一個不斷遞增的浮點數。爲什麼?

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返回正確的textureRegion是至關重要的。我想這與循環同一動畫一直有關。我可以要求關於這個話題的更多解釋嗎?

回答

1

傳遞給渲染的增量值是自上一幀以來的時間量,因此每次調用渲染時將其添加到animationTimer意味着animationTimer保存自動畫開始運行以來的時間量。

這是確定在任何給定時間要顯示哪個幀所需的。所以如果你想讓你的動畫以每秒5幀的速度運行,並且animationTimer是400ms,那麼它可以計算出它應該顯示幀數2.

它每次調用render時都不會簡單地選擇下一幀因爲這會將動畫速度與幀率結合起來,這並不總是令人滿意的。此外,框架並不是不變的,所以這種方法可以確保平滑任何不一致。

+0

是的,這是有道理的。謝謝! – brumbrum

相關問題