Skip to content

Instantly share code, notes, and snippets.

@tedigc
Last active September 20, 2016 19:03
Show Gist options
  • Select an option

  • Save tedigc/e48b24180cc5f2f31309 to your computer and use it in GitHub Desktop.

Select an option

Save tedigc/e48b24180cc5f2f31309 to your computer and use it in GitHub Desktop.
Used for animated sprites in libGDX, but could easily be changed for some other framework.
import com.badlogic.gdx.graphics.g2d.Sprite;
/**
* @author tedigc
* @since 8-04-2016
*/
public class Animation {
private Sprite[] frames;
private int currentFrame;
private boolean playedOnce;
public Timer timer = new Timer(0);
public Animation(Sprite[] frames, float delay) {
this.frames = frames;
this.timer = new Timer(delay, true);
}
public void setFrames(Sprite[] frames) {
this.frames = frames;
currentFrame = 0;
playedOnce = false;
timer.resetTimer();
timer.start();
}
public void tick(float dt) {
// Check if it's time to change frame
if(timer.tick(dt)) {
this.currentFrame++;
}
// If at the end, loop back to the first frame.
if(currentFrame >= frames.length) {
currentFrame = 0;
playedOnce = true;
}
}
public void setDelay(float delay) {
timer.setDelay(delay);
}
public void setFrame(int i) {
this.currentFrame = i;
}
public int getCurrentFrame() {
return this.currentFrame;
}
public int getNFrames() {
return this.frames.length;
}
public Sprite getCurrentSprite() {
return frames[currentFrame];
}
public boolean hasPlayedOnce() {
return this.playedOnce;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment