Foundation.TextureAnimation

This type is the animated form of a TextureRegion, and is used to create flip-book animations. Texture animations are made up of one or more AnimationSequences which can be played in a variety of ways.


Example

// Create a texture (9 regions in 3*3 grid)
var texture = new Foundation.Texture('9numbers.png', 300, 300);
    texture.createRegionsFromGrid(3, 3);

// Create the Texture Animation (giving it the name "numbers")
texture.createAnimation('numbers', theGameTimer);

// Create sequence of all regions in forward direction
texture.getOriginalAnimationReference('numbers').createSequence(
  'forward', // name the sequence 'forward' 
  ['0','1','2','3','4','5','6','7','8'], // All regions (names auto-generated by create from grid)
  0.25, // 250ms per frame
  'reverse' // Play the reverse sequence when this one finishes
);

// As above, but in reverse direction
texture.getOriginalAnimationReference('numbers').createSequence(
  'reverse', // name the sequence 'reverse' 
  ['8','7','6','5','4','3','2','1','0'], // All regions (names auto-generated by create from grid)
  0.25, // 250ms per frame
  'forward' // Play the forward sequence when this one finishes
);

// Create a sprite and set it's image to be the numbers animation
var sprite = new Foundation.Sprite(
  new Foundation.Rectangle(0, 0, 100, 100),
  texture.getAnimation('numbers');
);

// Start the forward sequence playing. Playback will
// infinitely loop between the forward and reverse sequences
(<Foundation.TextureAnimation>sprite.image).play('forward');


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/actors/_digest.ts'
  • '/foundation/core/actors/TextureRegion.ts'

Extends

This type extends:

equals() always returns false. clone() is deep, but does not preserve the animation state. If the onFrameChange callback is set, a reference to the same function will be set in the cloned item, and it's context will be preserved. Manual rebinding of context may be required after the clone operation, depending on needs and use.

Implements

This type does not implement any interfaces (except those in the base).


Constructors

(texture: Texture, timer: GameTimeTracker)

  • Pass the texture the animation is attached to, and a game time tracker object.

Public Properties

onFrameChange: Function

  • A function to call when a frame change occurs. Will be passed the name of the texture region being used for the new frame. Useful for changing colliders based on the current frame, etc.

Instance Methods

createSequence(name: string, frames: string[], secsPerFrame?: number, next?: string)

  • Creates a new sequence in the animation.
  • A sequence is a collection of frames which make up a distinct animation (for example, the "run" animation).
  • TextureAnimations can be made up of multiple sequences (e.g. the "idle", "run", and "jump" sequences).
  • name: A unique string id for the sequence
  • frames: An array of the texture regions which make up the sequence (in linear order).
  • secsPerFrame: The number of seconds each frame is played for (more precisely, the minimum time each frame is played for).
  • next: The name / string id of the sequence to play when the current sequence is finished. Set to the same as the name property for a looping sequence, or null for a single-shot sequence.

play(name: string): boolean

  • Play the names animation sequence.
  • name will be the name property from one of the created animation sequences.
  • Returns true if it was possible to begin playback of the named sequence.

stop(name?: string): void

  • Stop the current animation sequence.
  • Optionally, provide the name of a texture region (note: region, not sequence) to jump to. Any region in the current texture can be used. If the onFrameChange callback is defined, it will be triggered.

setStaticRegion(name: string): boolean

  • Jumps the animation to the named texture region. Any region in the current texture can be used.
  • If the onFrameChange callback is defined, it will be triggered.
  • If the animation is still playing, the next frame change will update the region as normal.
  • This method is mostly used internally, but has been made public for convenience. As a general rule, though, the play() or stop() (with the jump property) methods are preferable (both call this method internally).