Files
flame/doc/examples/sprite_batch/lib/main_auto_loading.dart
Erick 4db3c0e401 Enabling direct import of Sprite and SpriteAnimation (#652)
* Enabling direct import of Sprite and SpriteAnimation

* Renan follow up

* Fixing spritesheet exmaple

Co-authored-by: Erick Zanardo <erickzanardoo@gmail.com>
2021-02-03 14:20:39 -03:00

62 lines
1.4 KiB
Dart

import 'dart:math';
import 'package:flame/sprite.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:flame/game.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final game = MyGame();
runApp(
GameWidget(
game: game,
),
);
}
class MySpriteBatchComponent extends SpriteBatchComponent
with HasGameRef<MyGame> {
@override
Future<void> onLoad() async {
spriteBatch = await gameRef.loadSpriteBatch('boom3.png');
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,
);
const NUM = 100;
final r = Random();
for (int i = 0; i < NUM; ++i) {
final sx = r.nextInt(8) * 128.0;
final sy = r.nextInt(8) * 128.0;
final x = r.nextInt(gameRef.size.x.toInt()).toDouble();
final y =
r.nextInt(gameRef.size.y ~/ 2).toDouble() + gameRef.size.y / 2.0;
spriteBatch.add(
source: Rect.fromLTWH(sx, sy, 128, 128),
offset: Vector2(x - 64, y - 64),
);
}
}
}
class MyGame extends BaseGame {
SpriteBatch spriteBatch;
@override
Future<void> onLoad() async {
add(MySpriteBatchComponent());
}
}