SpriteAnimation.fromAsepriteData constructor

SpriteAnimation.fromAsepriteData(
  1. Image image,
  2. Map<String, dynamic> jsonData
)

Automatically creates an Animation Object using animation data provided by the json file provided by Aseprite.

image: sprite sheet animation image. jsonData: animation's data in json format.

Implementation

factory SpriteAnimation.fromAsepriteData(
  Image image,
  Map<String, dynamic> jsonData,
) {
  final jsonFrames = jsonData['frames'] as Map<String, dynamic>;
  return SpriteAnimation(
    jsonFrames.values.map((dynamic value) {
      final map = value as Map;
      final frameData = map['frame'] as Map<String, dynamic>;
      final x = frameData['x'] as int;
      final y = frameData['y'] as int;
      final width = frameData['w'] as int;
      final height = frameData['h'] as int;
      final stepTime = (map['duration'] as int) / 1000;
      final sprite = Sprite(
        image,
        srcPosition: Vector2Extension.fromInts(x, y),
        srcSize: Vector2Extension.fromInts(width, height),
      );
      return SpriteAnimationFrame(sprite, stepTime);
    }).toList(),
  );
}