Skip to main content

Composition

Composition helpers wrap other actions without subclassing. They implement the same Action interface, so you can nest them arbitrarily and pass the result to entity.runAction().

Minimal example​

Runnable sample: website/snippets/actions/composition.ts.

import { moveBy, rotateBy, sequence, parallel, repeat, repeatForever, delay } from '@zylem/game-lib/actions';

patrol.runAction(
repeat(
sequence(
parallel(
moveBy({ x: 3, duration: 1500 }),
rotateBy({ y: 90, duration: 1500 }),
),
delay(250),
moveBy({ x: -3, duration: 1500 }),
),
3,
),
);

spinner.runAction(repeatForever(rotateBy({ y: 360, duration: 2000 })));

Semantics​

HelperDurationCompletes when
sequence(...actions)Sum of child durationsLast child finishes
parallel(...actions)Max child durationAll children finish
repeat(action, times)action.duration × timesRan times cycles
repeatForever(action)InfinityNever

sequence forwards leftover delta time when a child finishes mid-frame, so chained short actions stay in sync with the frame clock.

Pitfalls​

  • repeatForever never sets done; use entity.clearActions() or keep it on a dedicated entity you destroy.
  • Empty sequence() completes immediately.
  • parallel with mixed zero-duration and long-duration children still waits for the longest child.

API reference​