mwg API
    Preparing search index...

    Class SpriteMotion

    Squash, hop, bob, wobble, spin, shear and flip over a display object's transform, each running through a Tweener so reduced motion is honoured once, here, instead of by every game's own scale/Math.sin write.

    The object's transform at construction is its rest pose. Every running effect keeps its own contribution and the transform is recomputed from the rest pose and all contributions, so effects compose (a hop with a squash) and never accumulate drift: when one ends, is cancelled, or the whole motion is destroyed, its share simply disappears. Rotation and shear turn about the object's origin, so give a sprite a centred anchor (or pivot) first. Sequence effects behind a game event by awaiting a handle's done.

    Which effect plays on which event, and how much, is the game's business; this only moves.

    import { SpriteMotion } from '@datamoc/mwg/two-d/render';

    const rat = { x: 0, y: 0, rotation: 0, scale: { x: 1, y: 1 }, skew: { x: 0, y: 0 } };
    const motion = new SpriteMotion(rat);

    motion.hop({ height: 6, duration: 0.3 }); // up and back down
    motion.squash({ amount: 0.2, duration: 0.3 }); // composes with the hop
    motion.flip('x'); // face the other way, composes with both
    motion.update(1 / 60); // in the game loop

    motion.destroy(); // back to the rest pose, flip included
    Index
    • A sinusoidal vertical offset of amplitude. With cycles it plays that many periods and ends; without, it loops until cancelled (breathing, a floating item). Under reduced motion a looping bob holds still.

      Parameters

      • options: { amplitude: number; cycles?: number; period: number }

      Returns MotionHandle

    • Mirrors along axis ('x' turns left to right). A state, not an animation: it composes with whatever is running and is undone by destroy. Pass mirrored to set it outright rather than toggle; returns the new state.

      Parameters

      • axis: "x" | "y"
      • Optionalmirrored: boolean

      Returns boolean

    • advances the effects; call from the game loop (the tweener part is skipped for a shared one)

      Parameters

      • dt: number

      Returns void