Skip to content

Animated Sprite Textures

Arnaud Guyon edited this page May 16, 2018 · 3 revisions

It is possible to create animated textures for sprites, like gif. The idea is to create a big texture including all images of the animation, and then create frames with some parts of that texture.

Let's animate a medusa! We will create a png with 3 images, and create 4 frames with several durations (the middle position is played twice).

  • frame 1: left picture for 1.25 seconds
  • frame 2: middle picture for 0.25 seconds
  • frame 3: right picture for 0.50 seconds
  • frame 4: middle picture for 0.10 seconds (then it will cycle).

Your sprite must inherit from AnimatedSprite. You will be then able to call addFrame on it.

addFrame(float duration, float uStart, float vStart, float uStop, float vStop)

public class Medusa extends AnimatedSprite {

    public Medusa(int width, int height) {
        super(width, height);

        addFrame(1.25f, 0/3f,0, 1/3f, 1);
        addFrame(0.25f, 1/3f,0, 2/3f, 1);
        addFrame(0.50f, 2/3f,0, 3/3f, 1);
        addFrame(0.10f, 1/3f,0, 2/3f, 1);
    }

Note: if you don't want to set a duration for a frame, just put 0 for duration, and manually call setFrame(int frameNumber) when you want to display a given frame.

Clone this wiki locally