Skip to content

Instantly share code, notes, and snippets.

@mgsx-dev
Last active January 16, 2019 20:36
Show Gist options
  • Save mgsx-dev/9042ed0b755960fd221b849481a3cca6 to your computer and use it in GitHub Desktop.
Save mgsx-dev/9042ed0b755960fd221b849481a3cca6 to your computer and use it in GitHub Desktop.
libgdx scene2d image with animation capability
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
public class ImageAnimation extends Image
{
private Animation<TextureRegion> animation;
private float time;
protected float speed = 1f;
protected TextureRegionDrawable drawable = new TextureRegionDrawable();
public ImageAnimation() {
super();
setDrawable(drawable);
}
public void setAnimation(Animation<TextureRegion> animation) {
this.animation = animation;
}
public void setPose(TextureRegion textureRegion) {
drawable.setRegion(textureRegion);
setDrawable(drawable);
invalidateHierarchy();
setSize(getPrefWidth(), getPrefHeight());
invalidate();
this.animation = null;
}
@Override
public void act(float delta)
{
time += delta * speed;
if(animation != null && animation.getAnimationDuration() > 0){
TextureRegion frame = animation.getKeyFrame(time, true);
drawable.setRegion(frame);
setDrawable(drawable);
invalidateHierarchy();
invalidate();
}else{
// setDrawable(null);
}
super.act(delta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment