TweenSystem. Tween

A tween: drives a callback with a value interpolated between start and end over duration seconds. Pauses with the game by default.

  • In TypeScript it is a Tween<T> of the type it tweens, which comes from start and end or the callback's parameter, so (v: number)=> ... takes a number

Constructor

new Tween(callback, startopt, endopt, durationopt, optionsopt)

Create a new tween. The callback fires immediately with start so the target snaps to the start value on the same frame the tween is created.

start and end may be numbers, Vector2, Vector3 or Color instances, or any object exposing a lerp(other, percent) => sameType method. The callback receives the interpolated value (a number, or a fresh instance for lerp-able types). Both endpoints must be the same type.

Parameters:
NameTypeAttributesDefaultDescription
callbackfunction

Called with the interpolated value each frame

startT<optional>
0

Starting value

endT<optional>
1

Ending value

durationnumber<optional>
1

Duration in seconds

optionsObject<optional>
Properties
NameTypeAttributesDefaultDescription
easefunction<optional>

Easing function (defaults to LINEAR)

useRealTimeboolean<optional>
false

Advance even when the game is paused (matches Timer's useRealTime)

pausedboolean<optional>
false

Start in paused state

Example
// Animate a fade-out over 2 seconds with an ease-out sine curve.
 new Tween((v) => obj.alpha = v, 1, 0, 2, { ease: Ease.OUT(Ease.SINE) });

Members

callback :function

Type:
  • function
Properties
TypeDescription
function

Called with the interpolated value each frame

duration

Properties
TypeDescription
number

Total duration in seconds

ease

Properties
TypeDescription
function

Easing curve mapping [0,1] -> [0,1]

end :T

Type:
  • T
Properties
TypeDescription
T

Ending value

end :any

Type:
  • any

life

Properties
TypeDescription
number

Remaining time in seconds (counts down from duration to 0)

onComplete :undefined|function

Type:
  • undefined | function
Properties
TypeDescription
undefined | function

Called once the tween completes: when its last pass ends, the last iteration of a loop or pingPong, and again each time a restart plays through; then() sets it

paused

Properties
TypeDescription
boolean

If true, stop advancing until cleared

start :T

Type:
  • T
Properties
TypeDescription
T

Starting value

start :any

Type:
  • any

target

Properties
TypeDescription
Object | undefined

The object tweenProperty animates, the tween stops once it is destroyed, even while paused

useRealTime

Properties
TypeDescription
boolean

If true, advance even when the game is paused

Methods

getPercent() → {number}

Get how far this tween has progressed, from 0 (just started) to 1 (completed). Clamped — overshoot past completion still reads 1.

Returns:
Type: 
number

getValue() → {T}

Get the current interpolated value (the value most recently passed to the callback). Returns a number, Vector2, Vector3 or Color depending on the tween's start/end types.

Returns:
Type: 
T

interp(life) → {T}

Compute the interpolated value at the given remaining life. At life === duration the result is start; at life === 0 it is end.

  • At life 0 it is the end value exactly
  • A vector goes past its ends as far as the easing does, as a number does; a Color stays between them, so its channels stay in range, and any other type goes as far as its own lerp takes it
Parameters:
NameTypeDescription
lifenumber
Returns:
Type: 
T

isActive() → {boolean}

True if this tween is in the active list and not paused.

Returns:
Type: 
boolean

loop(countopt) → {Tween.<T>}

Repeat this tween n total times. After each iteration finishes, the same tween starts over, so the handle returned stays good for the whole loop: pause or stop it to pause or stop every iteration left. loop() with no argument loops forever.

Mutually exclusive with pingPong; calling either replaces the other. A then callback, set before or after, is called when the last iteration ends.

Parameters:
NameTypeAttributesDefaultDescription
countnumber<optional>
Infinity
Returns:
Type: 
Tween.<T>

pause()

Pause this tween. While paused, tweenUpdate skips it.

pingPong(countopt) → {Tween.<T>}

Like loop, but swap start and end between iterations so the value bounces back and forth. pingPong() with no argument bounces forever.

Mutually exclusive with loop; calling either replaces the other. A then callback, set before or after, is called when the last iteration ends.

Parameters:
NameTypeAttributesDefaultDescription
countnumber<optional>
Infinity
Returns:
Type: 
Tween.<T>

restart()

Reset this tween to the start: life back to duration, pause cleared, re-added to the active list if previously stopped, and the callback re-fired with the start value. It replays one pass: a loop or pingPong that has finished is not started over, a pingPong that ended on its way back plays that way again, and a restart mid loop keeps the iterations left. Call loop or pingPong again after restart to repeat it. The then callback is kept and is called again when it completes.

resume()

Resume a paused tween.

setEase(easeFn) → {Tween.<T>}

Set the easing curve and return this for chaining.

Parameters:
NameTypeDescription
easeFnfunction
Returns:
Type: 
Tween.<T>

stop()

Remove this tween from the active list, ending a loop or pingPong too, without calling the then-callback. It keeps the then-callback, so a restart calls it when it completes.

then(callback) → {Tween.<T>}

Set the completion callback, onComplete, and return this for chaining. It is called once the tween completes: when its pass ends, or for a loop or pingPong when its last iteration ends, so an endless one never calls it. Calling then again replaces the previous callback.

  • It works with loop and pingPong in either order, neither replaces the other
  • It is kept by restart, so a restarted tween calls it again when it completes
  • stop and tweenStopAll end a tween without calling it
Parameters:
NameTypeDescription
callbackfunction
Returns:
Type: 
Tween.<T>