Skip to content

Timelines

⚠️ Advanced Topic
This section is intended for students who are comfortable with the basics of object-oriented programming. You should already know how to define classes, create objects, and work with object properties. If you are new to OOP, we recommend you to stick to using the methods mentioned in the interpolation section.

When creating animations, you often want to sequence multiple Tweens or other animations, or sometimes play some animations in parallel. Doing this manually can be cumbersome and error-prone. A timeline allows you to compose multiple animations into a single sequence, giving you precise control over when each animation starts and ends.

In our system, a timeline, can consist of Tweens, texture animations or of other timelines. You can nest timelines inside other timelines, so complex sequences can be built from simple building blocks.

timelines also handle delays automatically. By default, animations in a timeline run sequentially in the order they are added. If you want two animations to run at the same time, you can use a negative delay for the second one, effectively overlapping it with the previous animation.

timelines can also be reset and reversed, which makes it easy to replay or undo a sequence of animations.

Creating a timeline

First, you create a timeline object:

timeline timeline = new timeline();

Or, using the create() method (useful for chaining, like with the Tweens):

timeline timeline = timeline.create();

Then, you add Tweens, timelines or texture animations to it. By default, animations are added sequentially:

timeline.add(tween1);
timeline.add(tween2);
timeline.add(tween3);
  • tween1 runs first.
  • tween2 starts after tween1 finishes.
  • tween3 starts after tween2 finishes.

Adding Animations With Delays

You can specify a delay (in seconds) for each animation:

timeline.add(tween1, 0);   // starts immediately
timeline.add(tween2, 0.5f); // starts 0.5 seconds after the timeline starts
timeline.add(tween3, -0.2f); // starts 0.2 seconds *before* tween2 ends
  • Positive delay → wait before starting.
  • Negative delay → start overlapping with previous animations.

Updating the timeline

Like Tweens, timelines need to be updated each frame to advance the animations:

@Override
public void render(float delta) {
    timeline.update(delta); // updates all contained animations
}

Timeline has finished

You can poll whether the timeline has finished:

if (timeline.isFinished()) {
    System.out.println("timeline finished!");
}

Resetting and Reversing

timelines support reset() and reset(true) to play all animations again:

// Replay in normal order
timeline.reset();
// Replay in reverse order
timeline.reset(true);

When reversing, the timeline plays the animations backward. Delays and durations are properly handled, so animations start and end in the correct reversed order.

Full example

This is an example of creating a timeline animation, where two dice are animated by one timeline. The animation for the opponentDice runs after the playerDice animation has finished.

public class ExampleScreen extends ScalableGameScreen {
    // These are the dice we want to animate
    Dice playerDice = new Dice(0, 0, 200);
    Dice opponentDice = new Dice(0, 0, 200);

    // This is where we store the timeline
    Timeline animationTimeline;

    @Override
    public void show() {
        // Create a timeline and add multiple tweens sequentially
        animationTimeline = Timeline.create()
            // Animate playerDice
            .add(Tween.on(playerDice)
                       .to("x", 300)
                       .to("y", 150)
                       .to("size", 120)
                       .duration(1f)
                       .ease(Interpolation.pow2Out))
            // Animate opponentDice after playerDice finishes
            .add(Tween.on(opponentDice)
                       .to("x", 500)
                       .to("y", 200)
                       .to("size", 100)
                       .duration(1f)
                       .ease(Interpolation.pow2Out));
    }

    @Override
    public void render(float delta) {
        super.render(delta);

        // Update the timeline (this updates all tweens in sequence)
        animationTimeline.update(delta);

        // Reverse the timeline if finished
        if (animationTimeline.isFinished()) {
            animationTimeline.reset(true);
        }

        // Clear screen and draw dice
        GameApp.clearScreen();
        GameApp.drawRectCentered(playerDice.x, playerDice.y, playerDice.size, playerDice.size, "white");
        GameApp.drawRectCentered(opponentDice.x, opponentDice.y, opponentDice.size, opponentDice.size, "red");
    }
}

Notes

  • Animations run sequentially by default.
  • Use delays to start animations later or earlier, allowing parallelism.
  • update(delta) must be called every frame for all animations in the timeline.
  • You can poll with isFinished() to detect when the full sequence is done.
  • reset(true) allows the entire timeline to be played in reverse, including handling delays properly.