Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX is there an actor that is animated?

Tags:

java

libgdx

In LibGDX Is there an actor that is animated (takes an Animation) and when added to a Stage animates itself or do you have to implement your own Image class in and animate it yourself?

like image 909
Lokiare Avatar asked Apr 17 '13 12:04

Lokiare


1 Answers

I simply created an "AnimatedImage" actor class which only takes an Animation as an argument (no need for a custom Drawable class). I think this solution is much simpler than the one above.

AnimatedImage.java:

public class AnimatedImage extends Image
{
    protected Animation animation = null;
    private float stateTime = 0;

    public AnimatedImage(Animation animation) {
        super(animation.getKeyFrame(0));
        this.animation = animation;
    }

    @Override
    public void act(float delta)
    {
        ((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime+=delta, true));
        super.act(delta);
    }
}
like image 70
potpiejimmy Avatar answered Oct 09 '22 02:10

potpiejimmy