static async fromAsset factory method | optional add all values in constructor

This commit is contained in:
Erlend Fagerheim
2020-04-19 09:41:59 +02:00
parent dc290df280
commit 55a358dc74
3 changed files with 13 additions and 19 deletions

View File

@ -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),

View File

@ -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) {}
}

View File

@ -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<Rect> rects = [];
List<RSTransform> transforms = [];
List<Color> 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<SpriteBatch> 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,