melonJS
    Preparing search index...

    Class FrameAnimation

    The shared frame-animation engine behind Sprite (2D) and Sprite3d (3D billboards). It owns the animation state — definitions, the current frame, timing, looping and chaining — everything independent of how a frame is ultimately drawn, and drives its host renderable through a small contract:

    • it reads the host's resolved texture (host.source, host.textureAtlas, host.atlasIndices) to turn a frame index/name into a region;
    • it calls the applyFrame(region) callback passed to the constructor whenever the frame changes — the host applies it to its own geometry (Sprite swaps its source sub-texture, size and anchor; Sprite3d maps the region onto its quad's UVs + vertices) and marks itself dirty there;
    • it fires host.onended() at each cycle end.

    The engine owns no dirty flag: a frame change flows through applyFrame, where the host sets its own isDirty. Callers read dirtiness from the host (the host's update() returns super.update()isDirty).

    Hosts expose the public-facing state (anim, current, animationspeed, animationpause, …) as thin accessors onto this engine, so 2D and 3D frame animation share one implementation with no behavioral fork.

    Index

    Constructors

    • Parameters

      • host: object

        the renderable this engine drives. Read for the texture it animates (source / textureAtlas / atlasIndices) and the mutable cycle-end callback (onended). These are read lazily (the texture is usually resolved after the engine is constructed), which is why they live on the host rather than being passed in. The engine does not touch isDirty — the host marks itself dirty inside applyFrame.

      • applyFrame: (region: object) => void

        called whenever the frame changes, with the selected texture region; the host applies it to its own geometry (a Sprite swaps its sub-texture / size / anchor, a Sprite3d remaps its quad's UVs + vertices).

      Returns FrameAnimation

    Properties

    anim: object

    defined animations, keyed by id

    animationpause: boolean

    pause flag — freezes the current frame

    animationspeed: number

    default frame cycling speed (ms between frames)

    current: object

    current frame info

    dt: number

    elapsed time within the current frame, in ms

    Methods

    • add an animation definition (see Sprite#addAnimation).

      Parameters

      • name: string

        animation id

      • index: string[] | number[] | object[]

        frame indices / names / objects

      • Optionalanimationspeed: number

        cycling speed in ms

      Returns number

      number of frames added (0 if no texture atlas)

    • clear the frame timer and the play-once "done" hold, without changing the current frame (the timer half of a stop, used by the video path which has no frame to rewind to).

      Returns void

    • select the active animation (see Sprite#setCurrentAnimation).

      Parameters

      • name: string

        animation id

      • OptionalresetAnim: string | object | Function

        loop / chain / completion behavior

      • Optionalpreserve_dt: boolean = false

        keep the elapsed-frame timer

      Returns object

      the host (for method chaining)

    • advance the frame animation by dt milliseconds, stepping frames, looping and chaining as configured (see Sprite#update). On a frame change the host marks itself dirty via _applyFrame (the engine owns no dirty flag); this returns whether a frame actually changed this tick — a finer-grained signal than the host's isDirty (which means "needs redraw for any reason").

      Parameters

      • dt: number

        elapsed time since the last update, in milliseconds

      Returns boolean

      true if a frame changed this tick