Phaser API Documentation

  Version: 
Filter

A Timeline is a way to schedule events to happen at specific times in the future.

You can think of it as an event sequencer for your game, allowing you to schedule the running of callbacks, events and other actions at specific times in the future.

A Timeline is a Scene level system, meaning you can have as many Timelines as you like, each belonging to a different Scene. You can also have multiple Timelines running at the same time.

If the Scene is paused, the Timeline will also pause. If the Scene is destroyed, the Timeline will be automatically destroyed. However, you can control the Timeline directly, pausing, resuming and stopping it at any time.

Create an instance of a Timeline via the Game Object Factory:

const timeline = this.add.timeline();

The Timeline always starts paused. You must call play on it to start it running.

You can also pass in a configuration object on creation, or an array of them:

const timeline = this.add.timeline({
    at: 1000,
    run: () => {
        this.add.sprite(400, 300, 'logo');
    }
});

timeline.play();

In this example we sequence a few different events:

const timeline = this.add.timeline([
    {
        at: 1000,
        run: () => { this.logo = this.add.sprite(400, 300, 'logo'); },
        sound: 'TitleMusic'
    },
    {
        at: 2500,
        tween: {
            targets: this.logo,
            y: 600,
            yoyo: true
        },
        sound: 'Explode'
    },
    {
        at: 8000,
        event: 'HURRY_PLAYER',
        target: this.background,
        set: {
            tint: 0xff0000
        }
    }
]);

timeline.play();

There are lots of options available to you via the configuration object. See the Phaser.Types.Time.TimelineEventConfig typedef for more details.

Constructor:

new Timeline(scene, config)

Parameters:

name type description
scene Phaser.Scene

The Scene which owns this Timeline.

config Phaser.Types.Time.TimelineEventConfig | Array.<Phaser.Types.Time.TimelineEventConfig>

The configuration object for this Timeline Event, or an array of them.

Since: 3.60.0
Source: src/time/Timeline.js (Line 13)

Members

complete: boolean
Focus
Focus

Description:

Whether the Timeline is complete (true) or not (false).

A Timeline is considered complete when all of its events have been run.

If you wish to restart a Timeline after it has completed, you can do so by calling the Timeline.restart method.

You can also use the Timeline.stop method to stop a running Timeline, at any point, without resetting it.

Type:
boolean
Default: false
Since: 3.60.0
Source: src/time/Timeline.js (Line 147)
Focus
Focus
elapsed: number
Focus
Focus

Description:

The elapsed time counter.

Treat this as read-only.

Type:
number
Since: 3.60.0
Source: src/time/Timeline.js (Line 118)
Focus
Focus

Description:

An array of all the Timeline Events.

Type:
Since: 3.60.0
Source: src/time/Timeline.js (Line 178)
Focus
Focus
paused: boolean
Focus
Focus

Description:

Whether the Timeline is running (true) or active (false).

When paused, the Timeline will not run any of its actions.

By default a Timeline is always paused and should be started by calling the Timeline.play method.

You can use the Timeline.pause and Timeline.resume methods to control this value in a chainable way.

Type:
boolean
Default: true
Since: 3.60.0
Source: src/time/Timeline.js (Line 129)
Focus
Focus
scene: Phaser.Scene
Focus
Focus

Description:

The Scene to which this Timeline belongs.

Type:
Since: 3.60.0
Source: src/time/Timeline.js (Line 100)
Focus
Focus

Description:

A reference to the Scene Systems.

Type:
Since: 3.60.0
Source: src/time/Timeline.js (Line 109)
Focus
Focus
totalComplete: number
Focus
Focus

Description:

The total number of events that have been run.

This value is reset to zero if the Timeline is restarted.

Treat this as read-only.

Type:
number
Since: 3.60.0
Source: src/time/Timeline.js (Line 165)
Focus
Focus

Methods

add(config)
Focus
Focus

Description:

Adds one or more events to this Timeline.

You can pass in a single configuration object, or an array of them:

const timeline = this.add.timeline({
    at: 1000,
    run: () => {
        this.add.sprite(400, 300, 'logo');
    }
});

Parameters:

name type description
config Phaser.Types.Time.TimelineEventConfig | Array.<Phaser.Types.Time.TimelineEventConfig>

The configuration object for this Timeline Event, or an array of them.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 451)
Focus
Focus
clear()
Focus
Focus

Description:

Removes all events from this Timeline, resets the elapsed time to zero and pauses the Timeline.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 531)
Focus
Focus
destroy()
Focus
Focus

Description:

Destroys this Timeline.

This will remove all events from the Timeline and stop it from processing.

This method is called automatically when the Scene shuts down, but you may also call it directly should you need to destroy the Timeline earlier.

Since: 3.60.0
Source: src/time/Timeline.js (Line 589)
Focus
Focus
getProgress()
Focus
Focus

Description:

Returns a number between 0 and 1 representing the progress of this Timeline.

A value of 0 means the Timeline has just started, 0.5 means it's half way through, and 1 means it's complete.

If the Timeline has no events, or all events have been removed, this will return 1.

If the Timeline is paused, this will return the progress value at the time it was paused.

Note that the value returned is based on the number of events that have been completed, not the 'duration' of the events (as this is unknown to the Timeline).

Returns:
Description:

A number between 0 and 1 representing the progress of this Timeline.

Type:
  • number
Since: 3.60.0
Source: src/time/Timeline.js (Line 564)
Focus
Focus
isPlaying()
Focus
Focus

Description:

Returns true if this Timeline is currently playing.

A Timeline is playing if it is not paused or not complete.

Returns:
Description:

true if this Timeline is playing, otherwise false.

Type:
  • boolean
Since: 3.60.0
Source: src/time/Timeline.js (Line 549)
Focus
Focus
pause()
Focus
Focus

Description:

Pauses this Timeline.

To resume it again, call the Timeline.resume method or set the Timeline.paused property to false.

If the Timeline is paused while processing the current game step, then it will carry on with all events that are due to run during that step and pause from the next game step.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 363)
Focus
Focus
play([fromStart])
Focus
Focus

Description:

Starts this Timeline running.

If the Timeline is already running and the fromStart parameter is true, then calling this method will reset the Timeline events as incomplete.

If you wish to resume a paused Timeline, then use the Timeline.resume method instead.

Parameters:

name type arguments Default description
fromStart boolean <optional> true

Reset this Timeline back to the start before playing.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 332)
Focus
Focus
preUpdate(time, delta)
Focus
Focus

Description:

Updates the elapsed time counter, if this Timeline is not paused.

Parameters:

name type description
time number

The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.

delta number

The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.

Since: 3.60.0
Source: src/time/Timeline.js (Line 199)
Focus
Focus
reset()
Focus
Focus

Description:

Resets this Timeline back to the start.

This will set the elapsed time to zero and set all events to be incomplete.

If the Timeline had any events that were set to once that have already been removed, they will not be present again after calling this method.

If the Timeline isn't currently running (i.e. it's paused or complete) then calling this method resets those states, the same as calling Timeline.play(true).

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 423)
Focus
Focus
resume()
Focus
Focus

Description:

Resumes this Timeline from a paused state.

The Timeline will carry on from where it left off.

If you need to reset the Timeline to the start, then call the Timeline.reset method.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 384)
Focus
Focus
stop()
Focus
Focus

Description:

Stops this Timeline.

This will set the paused and complete properties to true.

If you wish to reset the Timeline to the start, then call the Timeline.reset method.

Returns:
Description:

This Timeline instance.

Since: 3.60.0
Source: src/time/Timeline.js (Line 403)
Focus
Focus
update(time, delta)
Focus
Focus

Description:

Called automatically by the Scene update step.

Iterates through all of the Timeline Events and checks to see if they should be run.

If they should be run, then the TimelineEvent.action callback is invoked.

If the TimelineEvent.once property is true then the event is removed from the Timeline.

If the TimelineEvent.event property is set then the Timeline emits that event.

If the TimelineEvent.run property is set then the Timeline invokes that method.

If the TimelineEvent.target property is set then the Timeline invokes the run method on that target.

Parameters:

name type description
time number

The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.

delta number

The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.

Since: 3.60.0
Source: src/time/Timeline.js (Line 218)
Focus
Focus