getAnimation method

SpriteAnimation getAnimation(
  1. String selectionId
)

Returns a SpriteAnimation with the given selectionId.

Implementation

SpriteAnimation getAnimation(String selectionId) {
  final selection = selections[selectionId];

  final image = _assertImageLoaded();

  if (selection == null) {
    throw 'There is no selection with the id "$selectionId" on this atlas';
  }
  if (selection is! AnimationSelection) {
    throw 'Selection "$selectionId" is not an Animation';
  }

  final initialX = selection.x.toDouble();

  final frameSize = (1 + selection.w.toDouble()) / selection.frameCount;

  final width = frameSize * tileWidth;
  final height = (1 + selection.h.toDouble()) * tileHeight;

  final sprites = List.generate(selection.frameCount, (i) {
    final x = (initialX + i) * frameSize;
    return Sprite(
      image,
      srcPosition: Vector2(
        x * tileWidth,
        selection.y.toDouble() * tileHeight,
      ),
      srcSize: Vector2(width, height),
    );
  });

  return SpriteAnimation.spriteList(
    sprites,
    stepTime: selection.stepTime,
    loop: selection.loop,
  );
}