SpriteAnimationData.range constructor

SpriteAnimationData.range({
  1. required int start,
  2. required int end,
  3. required int amount,
  4. required List<double> stepTimes,
  5. required Vector2 textureSize,
  6. int? amountPerRow,
  7. Vector2? texturePosition,
  8. bool loop = true,
})

Specifies the range of the sprite grid.

Make sure your sprites are placed left-to-right and top-to-bottom

start is the start frame index. end is the end frame index.

Implementation

SpriteAnimationData.range({
  required int start,
  required int end,
  required int amount,
  required List<double> stepTimes,
  required Vector2 textureSize,
  int? amountPerRow,
  Vector2? texturePosition,
  this.loop = true,
})  : assert(amountPerRow == null || amount >= amountPerRow),
      assert(start <= end && start >= 0 && end <= amount),
      assert(stepTimes.length >= end - start + 1) {
  amountPerRow ??= amount;
  texturePosition ??= Vector2.zero();
  frames = List<SpriteAnimationFrameData>.generate(end - start + 1, (index) {
    final i = index + start;
    final position = Vector2(
      texturePosition!.x + (i % amountPerRow!) * textureSize.x,
      texturePosition.y + (i ~/ amountPerRow) * textureSize.y,
    );
    return SpriteAnimationFrameData(
      stepTime: stepTimes[index],
      srcPosition: position,
      srcSize: textureSize,
    );
  });
}