2012-11-21 27 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的例子..希望它会帮助你!