diff --git a/doc/examples/sprite_batch/lib/main.dart b/doc/examples/sprite_batch/lib/main.dart index 2fd661271..2b4f941a7 100644 --- a/doc/examples/sprite_batch/lib/main.dart +++ b/doc/examples/sprite_batch/lib/main.dart @@ -7,6 +7,7 @@ import 'package:flame/sprite_batch.dart'; import 'package:flame/components/sprite_batch_component.dart'; void main() async { + WidgetsFlutterBinding.ensureInitialized(); final Size size = await Flame.util.initialDimensions(); final game = MyGame(size); runApp(game.widget); @@ -17,17 +18,12 @@ class MyGame extends BaseGame { MyGame(Size screenSize) { size = screenSize; - } - - @override - void onAttach() { - super.onAttach(); initData(); } - void initData() { - spriteBatch = SpriteBatch('boom3.png'); + void initData() async { + spriteBatch = await SpriteBatch.withAsset('boom3.png'); spriteBatch.add( rect: const Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128), diff --git a/lib/components/sprite_batch_component.dart b/lib/components/sprite_batch_component.dart index 79df6de46..23dc590a3 100644 --- a/lib/components/sprite_batch_component.dart +++ b/lib/components/sprite_batch_component.dart @@ -4,7 +4,7 @@ import '../sprite_batch.dart'; import 'component.dart'; class SpriteBatchComponent extends Component { - SpriteBatch spriteBatch; + final SpriteBatch spriteBatch; BlendMode blendMode; Rect cullRect; Paint paint; @@ -26,9 +26,6 @@ class SpriteBatchComponent extends Component { ); } - @override - bool loaded() => spriteBatch?.atlas != null; - @override void update(double t) {} } diff --git a/lib/sprite_batch.dart b/lib/sprite_batch.dart index 202adfe26..dfeedcc13 100644 --- a/lib/sprite_batch.dart +++ b/lib/sprite_batch.dart @@ -4,8 +4,9 @@ import 'package:flutter/foundation.dart'; import 'flame.dart'; +/// sprite atlas with an image and a set of rects and transforms class SpriteBatch { - Image atlas; + final Image atlas; List rects = []; List transforms = []; List colors = []; @@ -16,17 +17,17 @@ class SpriteBatch { static final defaultTransform = RSTransform(1, 0, 0, 0); static const defaultColor = const Color(0x00000000); // transparent - SpriteBatch(String fileName) { - Flame.images.load(fileName).then((image) => atlas = image); + SpriteBatch(this.atlas); + + static Future withAsset(String imageName) async { + return SpriteBatch(await Flame.images.load(imageName)); } - SpriteBatch.fromImage(this.atlas); + int get width => atlas.width; - int get width => atlas?.width; + int get height => atlas.height; - int get height => atlas?.height; - - Size get size => atlas == null ? null : Size(width.toDouble(), height.toDouble()); + Size get size => Size(width.toDouble(), height.toDouble()); void addTransform({ @required Rect rect,