mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 20:36:31 +08:00
static async fromAsset factory method | optional add all values in constructor
This commit is contained in:
@ -7,6 +7,7 @@ import 'package:flame/sprite_batch.dart';
|
|||||||
import 'package:flame/components/sprite_batch_component.dart';
|
import 'package:flame/components/sprite_batch_component.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
final Size size = await Flame.util.initialDimensions();
|
final Size size = await Flame.util.initialDimensions();
|
||||||
final game = MyGame(size);
|
final game = MyGame(size);
|
||||||
runApp(game.widget);
|
runApp(game.widget);
|
||||||
@ -17,17 +18,12 @@ class MyGame extends BaseGame {
|
|||||||
|
|
||||||
MyGame(Size screenSize) {
|
MyGame(Size screenSize) {
|
||||||
size = screenSize;
|
size = screenSize;
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onAttach() {
|
|
||||||
super.onAttach();
|
|
||||||
|
|
||||||
initData();
|
initData();
|
||||||
}
|
}
|
||||||
|
|
||||||
void initData() {
|
void initData() async {
|
||||||
spriteBatch = SpriteBatch('boom3.png');
|
spriteBatch = await SpriteBatch.withAsset('boom3.png');
|
||||||
|
|
||||||
spriteBatch.add(
|
spriteBatch.add(
|
||||||
rect: const Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128),
|
rect: const Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128),
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import '../sprite_batch.dart';
|
|||||||
import 'component.dart';
|
import 'component.dart';
|
||||||
|
|
||||||
class SpriteBatchComponent extends Component {
|
class SpriteBatchComponent extends Component {
|
||||||
SpriteBatch spriteBatch;
|
final SpriteBatch spriteBatch;
|
||||||
BlendMode blendMode;
|
BlendMode blendMode;
|
||||||
Rect cullRect;
|
Rect cullRect;
|
||||||
Paint paint;
|
Paint paint;
|
||||||
@ -26,9 +26,6 @@ class SpriteBatchComponent extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
bool loaded() => spriteBatch?.atlas != null;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void update(double t) {}
|
void update(double t) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,9 @@ import 'package:flutter/foundation.dart';
|
|||||||
|
|
||||||
import 'flame.dart';
|
import 'flame.dart';
|
||||||
|
|
||||||
|
/// sprite atlas with an image and a set of rects and transforms
|
||||||
class SpriteBatch {
|
class SpriteBatch {
|
||||||
Image atlas;
|
final Image atlas;
|
||||||
List<Rect> rects = [];
|
List<Rect> rects = [];
|
||||||
List<RSTransform> transforms = [];
|
List<RSTransform> transforms = [];
|
||||||
List<Color> colors = [];
|
List<Color> colors = [];
|
||||||
@ -16,17 +17,17 @@ class SpriteBatch {
|
|||||||
static final defaultTransform = RSTransform(1, 0, 0, 0);
|
static final defaultTransform = RSTransform(1, 0, 0, 0);
|
||||||
static const defaultColor = const Color(0x00000000); // transparent
|
static const defaultColor = const Color(0x00000000); // transparent
|
||||||
|
|
||||||
SpriteBatch(String fileName) {
|
SpriteBatch(this.atlas);
|
||||||
Flame.images.load(fileName).then((image) => atlas = image);
|
|
||||||
|
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 => Size(width.toDouble(), height.toDouble());
|
||||||
|
|
||||||
Size get size => atlas == null ? null : Size(width.toDouble(), height.toDouble());
|
|
||||||
|
|
||||||
void addTransform({
|
void addTransform({
|
||||||
@required Rect rect,
|
@required Rect rect,
|
||||||
|
|||||||
Reference in New Issue
Block a user