Tweens
Animating properties over time in Phaser 4 -- TweenManager, creating tweens, tween config, easing functions, tween chains, stagger, yoyo, repeat, callbacks, and tween targets.
Key source paths: src/tweens/TweenManager.js
,
src/tweens/tween/Tween.js
,
src/tweens/tween/TweenChain.js
,
src/tweens/tween/BaseTween.js
,
,
,
,
Related skills: ../sprites-and-images/SKILL.md, ../animations/SKILL.md
Quick Start
js
// In a Scene's create() method:
const logo = this.add.image(100, 300, 'logo');
// Basic tween -- move the logo to x:600 over 2 seconds
this.tweens.add({
targets: logo,
x: 600,
duration: 2000,
ease: 'Power2'
});
is the scene's
instance, available in every Scene. The
method creates a tween, adds it to the manager, and starts playback immediately.
Core Concepts
Tween Lifecycle
Created -> Active (
) -> Start Delayed (
) -> Playing (
,
per frame) -> Yoyo/Repeat (
,
) -> Loop (
) -> Complete (
, then auto-destroyed unless
).
Fire-and-Forget Design
Tweens auto-destroy after completion. You do not need to store a reference unless you want to control them later. Set
in the config to keep a tween alive after completion for replay via
or
. You must manually call
on persisted tweens when done.
Targets
The
property accepts a single object, an array of objects, or a function that returns either. Targets are typically Game Objects but can be any JavaScript object with numeric properties. A tween will not manipulate any property that begins with an underscore.
js
// Single target
this.tweens.add({ targets: sprite, alpha: 0, duration: 500 });
// Multiple targets
this.tweens.add({ targets: [sprite1, sprite2, sprite3], y: 100, duration: 1000 });
Property Values
js
this.tweens.add({
targets: sprite,
x: 400, // absolute value
y: '-=100', // relative (subtract 100 from current)
rotation: '+=3.14', // relative (add to current)
alpha: { value: 0, duration: 300, ease: 'Cubic.easeIn' }, // per-property config
scale: [0.5, 1.5, 1], // array: interpolates through values over duration
angle: function (target, key, value, targetIndex, totalTargets, tween) {
return targetIndex * 90; // function: called once per target
},
duration: 1000
});
Array values use linear interpolation by default; override with the
config (
,
,
).
Common Patterns
Basic Tween
js
this.tweens.add({
targets: this.player,
x: 500,
y: 300,
duration: 1000,
ease: 'Sine.easeInOut'
});
Multiple Properties with Per-Property Config
js
this.tweens.add({
targets: this.enemy,
x: { value: 600, duration: 1500, ease: 'Bounce.easeOut' },
y: { value: 200, duration: 1000, ease: 'Power2' },
alpha: { value: 0.5, duration: 500, delay: 1000 }
});
Yoyo and Repeat
js
this.tweens.add({
targets: this.coin,
y: '-=50',
duration: 600,
ease: 'Sine.easeInOut',
yoyo: true, // returns to start value after reaching end
hold: 200, // pause 200ms at the end value before yoyo-ing back
repeat: -1, // -1 = infinite, 0 = play once, 1 = play twice, etc.
repeatDelay: 300 // pause 300ms before each repeat
});
controls how many extra times each property plays. A
of 1 means the tween plays twice total. The
property (on
) restarts the entire tween from scratch, including all properties. Use
for property-level looping and
for tween-level looping.
Stagger
Stagger offsets a value across multiple targets via
:
js
// 100ms delay between each target
delay: this.tweens.stagger(100)
// From center outward
delay: this.tweens.stagger(200, { from: 'center' })
// Range: distribute 0-1000ms across targets
delay: this.tweens.stagger([0, 1000])
// Grid stagger with easing
delay: this.tweens.stagger(500, { grid: [10, 6], from: 'center', ease: 'Cubic.easeOut' })
StaggerConfig: (offset),
(string/function),
(
/
/
/index),
([w, h]).
Tween Chain
A
plays tweens in sequence. Each tween in the chain starts after the previous one completes:
js
this.tweens.chain({
targets: this.player,
tweens: [
{ x: 300, duration: 1000, ease: 'Power2' },
{ y: 500, duration: 800, ease: 'Bounce.easeOut' },
{ scale: 2, duration: 500 },
{ alpha: 0, duration: 400 }
],
loop: 1, // loop the entire chain once (plays twice total)
loopDelay: 500,
onComplete: function () {
console.log('Chain finished');
}
});
Each entry in
is a standard
. Chain-level config supports
,
,
,
,
, and chain-level callbacks. Per-tween callbacks (
,
,
) belong on individual entries. Use
to append dynamically.
Relative Values
js
this.tweens.add({
targets: sprite,
x: '+=200', // add 200 to current x
y: '-=50', // subtract 50 from current y
angle: '+=180',
duration: 1000
});
Callbacks and Events
js
this.tweens.add({
targets: sprite,
x: 600,
duration: 2000,
// All callbacks receive (tween, targets, ...params)
onStart: function (tween, targets) { },
onUpdate: function (tween, targets) { }, // per-property, per-target, per-frame
onYoyo: function (tween, targets) { },
onRepeat: function (tween, targets) { },
onLoop: function (tween, targets) { },
onComplete: function (tween, targets) { },
onCompleteParams: ['extra', 'args'],
callbackScope: this
});
// Set callback after creation
tween.setCallback('onComplete', function (tween, targets) { }, []);
// Event emitter style (tweens extend EventEmitter)
tween.on('complete', function (tween, targets) { });
Number Tweens
A Number Tween has no target object. It tweens between two numeric values:
js
const counter = this.tweens.addCounter({
from: 0,
to: 100,
duration: 2000,
ease: 'Linear',
onUpdate: function (tween) {
const value = tween.getValue();
console.log(value); // 0 ... 100
}
});
Controlling and Killing Tweens
js
const tween = this.tweens.add({ targets: sprite, x: 600, duration: 2000, persist: true });
// Playback control
tween.pause(); tween.resume();
tween.stop(); // flags for removal; fires onStop
tween.restart(); // reset and replay from beginning
tween.seek(1000); // seek to 1000ms (suppresses events by default)
tween.complete(); // immediately complete; fires onComplete
tween.completeAfterLoop(0); // finish after current loop
tween.forward(500); tween.rewind(500);
tween.setTimeScale(0.5); // per-tween speed
tween.updateTo('x', 800); // change end value mid-tween
// Manager-level control
this.tweens.killAll(); // destroy all tweens
this.tweens.killTweensOf(sprite); // destroy tweens on target
this.tweens.isTweening(sprite); // boolean check
this.tweens.pauseAll(); this.tweens.resumeAll();
this.tweens.setGlobalTimeScale(0.5); // global speed
Configuration Reference
TweenBuilderConfig
| Property | Type | Default | Description |
|---|
| any / any[] | (required) | Object(s) to tween. |
| number | | Duration in ms. |
| number / function | | Delay before start (ms). Accepts . |
| string / function | | Easing function name or custom function. |
| array | | Parameters for parameterized easing (e.g. Elastic). |
| number | | Hold at end value before yoyo (ms). |
| number | | Per-property repeat count. = infinite. |
| number | | Delay before each repeat (ms). |
| boolean | | Reverse back to start after reaching end. |
| / | boolean | | Toggle flip on yoyo/repeat. |
| number | | Tween-level loop count. = infinite. |
| number | | Delay before each loop (ms). |
| number | | Delay before fires (ms). |
| boolean | | Start paused. Call to begin. |
| boolean | | Keep alive after completion. |
| object | -- | Explicit property config map (alt to top-level props). |
| string / function | -- | For array values: , , . |
| any | tween | context for callbacks. |
| Callbacks | function | -- | , , , , , , , , , . Each has matching array. |
TweenChainBuilderConfig
| Property | Type | Default | Description |
|---|
| any / any[] | -- | Default targets inherited by child tweens. |
| TweenBuilderConfig[] | (required) | Array of tween configs to play in sequence. |
| number | | Times to loop the entire chain. = infinite. |
| number | | Delay before each loop (ms). |
| number | | Delay before fires (ms). |
| boolean | | Start paused. |
| boolean | | Keep alive after completion. |
| Callbacks | function | -- | , , , , , , . |
NumberTweenBuilderConfig
| Property | Type | Default | Description |
|---|
| number | | Start value. |
| number | | End value. |
| number | | Duration in ms. |
| string / function | | Easing function. |
| All standard timing | -- | -- | , , , , , , , , , . |
| All standard callbacks | -- | -- | Same callback set as TweenBuilderConfig. |
TweenPropConfig (Per-Property Object)
Used when a property value is an object instead of a number/string. Supports:
,
,
,
,
,
,
,
,
,
,
,
,
,
. All override the tween-level defaults for that one property.
Easing Functions
All easing names are case-insensitive. Use the string name in the
config property.
Power Aliases
| Name | Equivalent |
|---|
| |
| (Quadratic Out) |
| |
| (Quartic Out) |
| (Quintic Out) |
Full Easing List
Each type supports
,
,
variants. The bare name defaults to Out.
Types: ,
,
,
,
,
,
,
,
,
.
Special: (no easing),
(discrete steps --
).
Custom: ease: function (t) { return t * t; }
where
is 0 to 1.
Events
Tweens (and TweenChains) extend
. You can listen via
:
| Event String | Constant | Fires When |
|---|
| Phaser.Tweens.Events.TWEEN_ACTIVE
| Tween added to manager |
| Phaser.Tweens.Events.TWEEN_START
| Playback begins (after delay) |
| Phaser.Tweens.Events.TWEEN_UPDATE
| Property updates each frame |
| Phaser.Tweens.Events.TWEEN_YOYO
| Property begins yoyo-ing back |
| Phaser.Tweens.Events.TWEEN_REPEAT
| Property repeats |
| Phaser.Tweens.Events.TWEEN_LOOP
| Entire tween loops |
| Phaser.Tweens.Events.TWEEN_COMPLETE
| Tween finishes |
| Phaser.Tweens.Events.TWEEN_STOP
| called |
| Phaser.Tweens.Events.TWEEN_PAUSE
| called |
| Phaser.Tweens.Events.TWEEN_RESUME
| called |
Event listener signature for Tween events:
. During seeking (
), events and callbacks are suppressed by default.
API Quick Reference
TweenManager ()
| Method | Returns | Purpose |
|---|
| | Create, add, and start a tween. |
| | Create multiple tweens at once. |
| | Create without adding. Use to add later. |
| | Create and start a sequential chain. |
| | Number tween (no target). |
| | Stagger function for delay/property values. |
| | Add a pre-created tween to the manager. |
| | Remove without destroying. |
| | Check if tween is in manager. |
| | All active tweens (copy). |
| | Tweens affecting a target. |
| | Is target being tweened? |
| / | | Destroy tweens. |
| / | | Pause/resume all tweens. |
| / | / | Global speed. |
| | Limit tick rate (default 240). |
setLagSmooth(limit, skip)
| | Configure lag smoothing. |
| | Iterate all tweens. |
Tween Instance
| Method | Purpose |
|---|
| / / | Playback control. |
| Stop and flag for removal. Fires . |
| Reset and replay. |
| Immediately complete. |
completeAfterLoop(loops?)
| Complete after N more loops. |
| Seek to ms offset. |
| / | Step forward/back. |
| / | Per-tween speed. |
setCallback(type, fn, params?)
| Set callback after creation. |
| Current TweenData value. |
updateTo(key, value, startToCurrent?)
| Change end value mid-tween. |
| / / | State checks. |
| / | Cleanup. |
Key properties:
,
,
,
(0-1),
(0-1 including loops),
,
,
,
,
(TweenData[]),
,
.
TweenChain Instance
Extends
. Has all tween playback methods (
,
,
,
,
,
, etc.) plus
to append tweens dynamically. Properties:
,
.
Gotchas
- Underscore properties are ignored. A tween will skip any property whose name starts with .
- Tweens auto-destroy. If you store a reference to a tween and try to use it after completion, it may be destroyed. Set to keep it alive, but you must it yourself when done.
- vs . is per-property (on TweenData). restarts the entire tween. A of 1 means the property plays twice total.
- means never fires. An infinitely looping tween never completes. Use to end it gracefully.
- Seeking suppresses events. When calling , events and callbacks are not dispatched unless you pass as the third argument.
- Stagger only works with multiple targets. Using on a single-target tween has no visible stagger effect.
- Destroyed targets. A tween completes early if its target has set to . Always clean up tweens before destroying the objects they reference, or rely on this auto-check.
- TweenManager.timeScale multiplies with Tween.timeScale. The effective speed is
managerTimeScale * tweenTimeScale
. Setting either to 0 freezes the tween.
- Duration cannot be zero. Internally clamped to a minimum of 0.01ms.
- TweenChain are inherited. If you set at the chain level, individual tweens in the chain inherit them unless they specify their own.
Source File Map
| File | Purpose |
|---|
src/tweens/TweenManager.js
| Scene plugin. , , , , etc. |
src/tweens/tween/Tween.js
| Individual tween. Targets, TweenData array, seeking, update. |
src/tweens/tween/TweenChain.js
| Sequential tween playback via ordered child Tweens. |
src/tweens/tween/BaseTween.js
| Shared base. EventEmitter, callbacks, state machine. |
src/tweens/tween/BaseTweenData.js
| Per-property state: duration, delay, yoyo, repeat, progress. |
src/tweens/tween/TweenData.js
| Numeric property tweening (extends BaseTweenData). |
src/tweens/tween/TweenFrameData.js
| Texture frame tweening (extends BaseTweenData). |
src/tweens/builders/TweenBuilder.js
| Parses config into Tween instance. |
src/tweens/builders/TweenChainBuilder.js
| Parses config into TweenChain instance. |
src/tweens/builders/NumberTweenBuilder.js
| Builds target-less number tweens. |
src/tweens/builders/StaggerBuilder.js
| Creates stagger functions. |
| Event constants (, , etc.). |
| JSDoc typedefs for all config objects. |
src/math/easing/EaseMap.js
| Maps ease string names to functions. |