flame SpriteSheet 创建动画的方式

100 阅读1分钟

1 第一种 图片是类似这种的图片

image.png

const String src = 'adventurer/animatronic.png';
await images.load(src);
var image = images.fromCache(src);
SpriteSheet sheet = SpriteSheet.fromColumnsAndRows(
  image: image,
  columns: 13,
  rows: 6,
);

int frameCount = sheet.rows * sheet.columns;
List<Sprite> sprites = List.generate(frameCount, sheet.getSpriteById);
SpriteAnimation animation = SpriteAnimation.spriteList(
  sprites,
  stepTime: 1 / 24,
  loop: true,
);

2 第二种

const int rowCount = 6;
const int columnCount = 13;
final Vector2 textureSize = Vector2(
  image.width / columnCount,
  image.height / rowCount,
);

SpriteAnimation animation = SpriteAnimation.fromFrameData(
  image,
  SpriteAnimationData.sequenced(
    amount: rowCount * columnCount,
    amountPerRow: columnCount,
    stepTime: 1 / 24,
    textureSize: textureSize,
  ),
);

3 第三种

 final playerImage = await game.images.load('icon_player.png');
    final SpriteSheet sheet = SpriteSheet.fromColumnsAndRows(
      image: playerImage,
      columns: 9,
      rows: 3,
    );
    animation = sheet.createAnimation(
      row: 0,
      to: 4,
      from: 2,
      stepTime: 0.1,
      loop: true
    );