Flutter&Flame游戏实践#15 | 生命游戏 - 演绎启动
    		       		warning:
    		            这篇文章距离上次修改已过445天,其中的内容可能已经有所变动。
    		        
        		                
                
import 'package:flame/components.dart';
 
class LifeGame extends BaseComponent {
  final size = Vector2.all(250);
  final List<Vector2> _cells = [];
 
  @override
  Future<void> onLoad() async {
    final sprite = await loadSprite('life_game_sprite.png');
    final cellSize = size / Vector2(sprite.amountX, sprite.amountY);
 
    for (var y = 0; y < sprite.amountY; y++) {
      for (var x = 0; x < sprite.amountX; x++) {
        final position = (Vector2(x, y) * cellSize);
        _cells.add(position);
        final animation = sprite.animation(row: y, column: x);
        add(SpriteAnimationComponent(
          animation: animation,
          size: cellSize,
          position: position,
        ));
      }
    }
  }
 
  // 其他生命游戏逻辑代码
}这个简化版本的LifeGame类展示了如何在Flame游戏中加载并初始化一个精灵动画组件。在onLoad方法中,我们首先加载精灵图片,并计算每个单元格的大小。然后,我们遍历精灵图片的每一行和每一列,为每个单元格创建一个精灵动画组件,并将其添加到游戏世界中。这个例子演绎了如何将图像资源集成到Flame游戏中的过程。
评论已关闭