12

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?

Lokiare
  • 1,238
  • 1
  • 15
  • 23

3 Answers3

22

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);
    }
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
potpiejimmy
  • 221
  • 2
  • 6
13

Just like you I didn't find animated Actor so I created myself:

AnimatedActor.java:

public class AnimatedActor extends Image
{
    private final AnimationDrawable drawable;

    public AnimatedActor(AnimationDrawable drawable)
    {
        super(drawable);
        this.drawable = drawable;
    }

    @Override
    public void act(float delta)
    {
        drawable.act(delta);
        super.act(delta);
    }
}

AnimationDrawable.java:

class AnimationDrawable extends BaseDrawable
{
    public final Animation anim;    
    private float stateTime = 0;

    public AnimationDrawable(Animation anim)
    {
        this.anim = anim;
        setMinWidth(anim.getKeyFrameAt(0).getRegionWidth());
        setMinHeight(anim.getKeyFrameAt(0).getRegionHeight());
    }

    public void act(float delta)
    {
        stateTime += delta;
    }

    public void reset()
    {
        stateTime = 0;
    }

    @Override
    public void draw(SpriteBatch batch, float x, float y, float width, float height)
    {
        batch.draw(anim.getKeyFrame(stateTime), x, y, width, height);
    }
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
Aliaaa
  • 1,590
  • 2
  • 16
  • 37
  • I did something similar. I'll wait and see if anyone can point out an actual Actor class, but if not I'll mark this as the answer. – Lokiare Apr 18 '13 at 07:08
  • Thank you very much for your snippet, kind sir. – Raphael Royer-Rivard Jul 04 '13 at 01:08
  • This seems a little bit slower then "direct" approach: TextureRegion frame2 = bird_07.getKeyFrame(stateTime, true); stage.getSpriteBatch().begin(); stage.getSpriteBatch().draw(currentFrame, 1000, 700); any idea why ? – atok Aug 19 '13 at 10:12
  • 4
    I think extending `Image` in this case is not really useful. You could extend `Actor` and store an `Animation` in it. Image wants to have `Drawable`s which IMHO are not as comfortable as `TextureRegion`s and others, and you don't need to make your own drawable. Also you store the data twice, in `Image` and in `AnimatedActor` – Robert P Feb 07 '14 at 13:15
  • @Mr.Developer it should work Libgdx is theoretically cross platform, but I didn't test it on iOS. – Aliaaa Nov 22 '15 at 09:04
  • I m try! This work on iOS if the texture is under the 4000x4000px – Mr. Developer Nov 22 '15 at 12:15
0

I didn't want to create another class so this is what I came up with this instead (If you just want a simple animated Actor):

Actor animatedActor = new Actor() {
    float time;
        
    @Override
    public void act( float delta ) {
        time += delta;
        super.act(delta);
    }

    @Override
    public void draw( Batch batch, float parentAlpha ) {
        batch.draw( searcAnimation.getKeyFrame( time ), getX(), getY(), getWidth(), getHeight() );
    }
};
animatedActor.setSize( 200, 200 );
stage.addActor( animatedActor );

Hopes this helps!

WASD123
  • 15
  • 1
  • 9