SpriteAnimationData.variable constructor

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

Takes some parameters and automatically calculate and create the frames for the sprite animation data.

amount The total amount of frames present on the image. stepTimes A list of times (in seconds) of each frame, should have a length equals to the amount parameter. textureSize The size of each frame. amountPerRow An optional parameter to inform how many frames there are on which row, useful for sprite sheets where the frames as disposed on multiple lines. texturePosition An optional parameter with the initial coordinate where the frames begin on the image, default to (top: 0, left: 0). loop An optional parameter to inform if this animation loops or has a single iteration, defaults to true.

Implementation

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