mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
This commit is contained in:
53
examples/lib/stories/sprites/spritebatch_example.dart
Normal file
53
examples/lib/stories/sprites/spritebatch_example.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SpritebatchExample extends FlameGame {
|
||||
static const String description = '''
|
||||
In this example we show how to render many sprites in a batch for
|
||||
efficiency, this is done with `SpriteBatch` and the `SpriteBatchComponent`.
|
||||
''';
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
final spriteBatch = await SpriteBatch.load('boom.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 (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),
|
||||
);
|
||||
}
|
||||
|
||||
add(
|
||||
SpriteBatchComponent(
|
||||
spriteBatch: spriteBatch,
|
||||
blendMode: BlendMode.srcOver,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user