Files
flame/examples/lib/stories/sprites/sprite_batch_load_example.dart
Luan Nico b79fee0ae2 chore: Update min Dart constraint to 3.8 (#3676)
Update min Dart constraint to 3.8, which will enable us to use the
fancier collection literals.

This requires bumping the min Flutter version as well:

<img width="1892" height="1122" alt="image"
src="https://github.com/user-attachments/assets/7c7b07fc-4d96-4987-824d-9a7133ecfb85"
/>
2025-08-10 12:42:31 -04:00

62 lines
1.7 KiB
Dart

import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/sprite.dart';
import 'package:flutter/material.dart';
class SpriteBatchLoadExample extends FlameGame {
static const String description = '''
In this example we do the same thing as in the normal sprite batch example,
but in this example the logic and loading is moved into a component that
extends `SpriteBatchComponent`.
''';
@override
Future<void> onLoad() async {
add(MySpriteBatchComponent());
}
}
class MySpriteBatchComponent extends SpriteBatchComponent
with HasGameReference<SpriteBatchLoadExample> {
MySpriteBatchComponent()
: super(
blendMode: BlendMode.srcOver,
);
@override
Future<void> onLoad() async {
final spriteBatch = await game.loadSpriteBatch('boom.png');
this.spriteBatch = spriteBatch;
spriteBatch.add(
source: const Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128),
offset: Vector2.all(200),
color: Colors.greenAccent,
scale: 2,
rotation: pi / 9.0,
anchor: Vector2.all(64),
);
spriteBatch.addTransform(
source: const Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128),
color: Colors.redAccent,
);
final size = game.size;
const num = 100;
final r = Random();
for (var i = 0; i < num; ++i) {
final sx = r.nextInt(8) * 128.0;
final sy = r.nextInt(8) * 128.0;
final x = r.nextInt(size.x.toInt()).toDouble();
final y = r.nextInt(size.y ~/ 2).toDouble() + size.y / 2.0;
spriteBatch.add(
source: Rect.fromLTWH(sx, sy, 128, 128),
offset: Vector2(x - 64, y - 64),
);
}
}
}