Tweens
Tweens animate any numeric property over time.
import { TweenTo } from 'bare-engine';
TweenTo(player, 0.5, { x: 300, rotation: 90 });TweenTo(object, seconds, props) is shorthand for bare.tween.To(...). Both return a tween with a promise, so you can wait for it:
await TweenTo(door, 1, { y: 0 }).promise;WARNING
Tweens only run once the scene has started, and a scene starts after its OnStart has finished. So don't await a tween inside a scene's OnStart: it would wait forever. Start the tween there without awaiting it, or await it later, for example from OnUpdate or an event. During development, reading .promise before the scene has started logs an error.
Example: a Platformer coin waits for its fade-out tween before removing itself.
Options
Add these to props next to the values you animate:
| Option | Description |
|---|---|
ease | An easing function, for example Easing.Back.Out. Default Easing.Quadratic.Out. |
delay | Seconds to wait before starting. With repeat or yoyo, it also waits before every repeat. |
initialDelay | Seconds to wait once, before the first pass only. Repeats then wait delay, which is 0 if not set. Useful with yoyo. |
yoyo | Play back and forth. |
repeat | Number of repeats, -1 for forever. |
onStart, onUpdate, onComplete, onRepeat, onStop | Callbacks. |
onEveryStart | Callback at the start of every pass, including repeats. |
persistent | Keep the tween when the scene is cleared. |
import { Easing, TweenTo } from 'bare-engine';
TweenTo(coin, 0.4, { innerY: '+=10', ease: Easing.Sinusoidal.InOut, yoyo: true, repeat: -1 });Relative values like '+=10' and '-=10' add to the current value.
By default, a new tween on the same object and property replaces the old one. Pass false as the fourth argument to keep both.
Example: the Showcase's tweens demo shows pulsing, spinning, bouncing, fading and swaying.
bare.tween
| Method | Description |
|---|---|
To(object, seconds, props, overwrite?) | Start a tween. |
Shake(object, property, duration, …) | Shake a property and settle back to 0. See Shake. |
SetTimer(seconds, onComplete) | Call a function after a delay. |
AddChain(...tweens) | Play tweens one after another. |
GetAllFrom(object) | All tweens running on an object. |
RemoveAllFrom(object) | Stop all tweens on an object. |
FinishAllFrom(object) / FinishAll() | Jump tweens to their end. |
RemoveTimers() | Cancel all timers. |
Example: the RPG's weapon checks GetAllFrom so it can't slash again mid-swing.
Shake
Shake sets a property to a starting value, then springs it back to 0 with a wobble. It suits properties that rest at 0, like innerRotation or an innerPosition axis:
bare.tween.Shake(enemy, 'innerRotation', 0.5, 20); // a quick wiggle| Parameter | Default | Description |
|---|---|---|
object, property | What to shake. | |
duration | Seconds until it's back at 0. | |
initialMove | -1 | The value it starts from. |
decay | 6 | How fast the wobble dies down. |
frequency | 4 | How many times it wobbles. |
phase | 0.075 | Shifts the wobble in time. |
amplitude | 40 | How far it overshoots. |
overwrite | true | Replace other tweens on the same property. |
Example: the Showcase shakes a heart with Shake(heart.innerPosition, "y", 1.2, -0.5, 3, 4, 0.075, 30).