play method

void play(
  1. T key, {
  2. int frame = 0,
  3. bool? loop,
})

Plays the entry key names, from the given frame.

// Animates the third entry of a sheet from its start.
sprite.play(2);

// Animates whichever entry a map calls 'jump'.
sprite.play('jump');

loop overrides what the entry states, until the next call. It also clears SpriteState.isFinished, so a non-looping sprite that already finished runs again from wherever this puts it.

Implementation

void play(
  T key, {
  int frame = 0,
  bool? loop,
}) {
  final entry = _sprite.resolve(key);

  if (entry == null) {
    throw ArgumentError.value(key, 'key', 'No such entry.');
  }

  if (frame < 0) {
    throw ArgumentError.value(
      frame,
      'frame',
      'Cannot be negative.',
    );
  }

  final frames = entry.frames;

  if (frame >= frames) {
    throw ArgumentError.value(
      frame,
      'frame',
      'That entry only plays $frames frames.',
    );
  }

  _current
    .._loop = loop
    .._elapsed = 0
    .._select(entry, frame);
}