2012-11-21 22 views
4

我想用libGDX實現一個簡單的動畫,而且我目前被困在一件事上。可以說我有一羣精靈需要一些時間才能完成。例如,像這樣的鏈接大約30個精靈:https://github.com/libgdx/libgdx/wiki/2D-Animation如何使用libgdx完成動畫

但是,在動畫完成之前,按下了某個鍵。對於流暢的動畫,我希望在開始下一組動畫之前完成30幀,以防止突然停止。

所以我的問題是如何在libGDX中實現它?我目前的想法是擴展Animation類,該類將跟蹤我具有的框架以及渲染了多少幀,然後顯示剩餘的幀。或者使用isAnimationFinished(float stateTime)函數(儘管我沒有運氣)。

我看到的例子像superjumper有很少的動畫,並沒有真正改變那麼多。

另外,有沒有辦法從TextureAtlas.createSprites方法中保存精靈列表並將它們與Animation類一起使用?如果不是,提供這個功能的目的是什麼?

感謝

+0

你能提供更多的關於你如何使用動畫的上下文嗎? isAnimationFinished(float stateTime)非常簡單,可能是您使用了錯誤的東西。當動畫完成「正常」時,你如何檢測? –

回答

5

您可以使用

animation.isAnimationFinished(stateTime); 

要查看您的動畫完成。

對於精靈:personnaly我用TextureRegion從TextureAtlas,我將它們存儲在陣列中的我的動畫

2

我創建一個類AnimatedImage擴展Image在圖像拼合自動化。我的代碼將是這樣的:

public class AnimatedImage extends Image{ 
    private Array<Array<Sprite>> spriteCollection; 
    private TextureRegionDrawable drawableSprite; 
    private Animation _animation; 
    private boolean isLooping; 
    private float stateTime; 
    private float currentTime; 


    public AnimatedImage(Array<Array<Sprite>> _sprites, float animTime, boolean _looping){ 
//  set the first sprite as the initial drawable 
     super(_sprites.first().first()); 
     spriteCollection = _sprites; 

//  set first collection of sprite to be the animation 
     stateTime = animTime; 
     currentTime = 0; 
     _animation = new Animation(stateTime, spriteCollection.first()); 

//  set if the anmation needs looping 
     isLooping = _looping; 
     drawableSprite = new TextureRegionDrawable(_animation.getKeyFrame(currentTime)); 
     this.setDrawable(drawableSprite); 
    } 

    public void update(float delta){ 
     currentTime += delta; 
     TextureRegion currentSprite = _animation.getKeyFrame(currentTime, isLooping); 
     drawableSprite.setRegion(currentSprite); 
    } 

    public void changeToSequence(int seq){ 
//  reset current animation time 
     resetTime(); 
     _animation = new Animation(stateTime, spriteCollection.get(seq)); 
    } 

    public void changeToSequence(float newseqTime, int seq){ 
     _animation = new Animation(newseqTime, spriteCollection.get(seq)); 
    } 

    public void setRepeated(boolean _repeat){ 
     isLooping = _repeat; 
    } 

    public boolean isAnimationFinished(){ 
     return _animation.isAnimationFinished(currentTime); 
    } 

    public void resetTime(){ 
      currentTime = 0; 
    } 


} 

changetosequence方法將使新Animation將用於在update方法來更新當前TextureRegionDrawable。當您致電changeToSequence時,resetTime將重置動畫的總時間。您可以添加事件偵聽器來調用changeToSequence方法。

這裏是例子:

private AnimatedImage _img; 

然後我加入InputListener這樣的:

_img.addListener(new InputListener(){ 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
       _img.changeToSequence(1); 
       return true; 

      } 
     }); 

希望它能幫助。

2

對這種動畫使用補間引擎。它的文檔記錄和libgdx支持它..谷歌關於它,你可以找到一堆使用libgdx的例子..希望它會幫助你!