Files
flame/examples/lib/stories/sprites/spritesheet_example.dart
Lukas Klingsbo 8b132d7c0b 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
2021-11-19 14:28:04 +01:00

80 lines
2.1 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/sprite.dart';
class SpritesheetExample extends FlameGame {
static const String description = '''
In this example we show how to load images and how to create animations from
sprite sheets.
''';
@override
Future<void> onLoad() async {
await super.onLoad();
final spriteSheet = SpriteSheet(
image: await images.load('spritesheet.png'),
srcSize: Vector2(16.0, 18.0),
);
final vampireAnimation =
spriteSheet.createAnimation(row: 0, stepTime: 0.1, to: 7);
final ghostAnimation =
spriteSheet.createAnimation(row: 1, stepTime: 0.1, to: 7);
final ghostAnimationVariableStepTimes =
spriteSheet.createAnimationWithVariableStepTimes(
row: 1,
to: 7,
stepTimes: [0.1, 0.1, 0.3, 0.3, 0.5, 0.3, 0.1],
);
final spriteSize = Vector2(80.0, 90.0);
final vampireComponent = SpriteAnimationComponent(
animation: vampireAnimation,
position: Vector2(150, 100),
size: spriteSize,
);
final ghostComponent = SpriteAnimationComponent(
animation: ghostAnimation,
position: Vector2(150, 220),
size: spriteSize,
);
final ghostAnimationVariableStepTimesComponent = SpriteAnimationComponent(
animation: ghostAnimationVariableStepTimes,
position: Vector2(150, 340),
size: spriteSize,
);
add(vampireComponent);
add(ghostComponent);
add(ghostAnimationVariableStepTimesComponent);
// Some plain sprites
final vampireSpriteComponent = SpriteComponent(
sprite: spriteSheet.getSprite(0, 0),
position: Vector2(50, 100),
size: spriteSize,
);
final ghostSpriteComponent = SpriteComponent(
sprite: spriteSheet.getSprite(1, 0),
size: spriteSize,
position: Vector2(50, 220),
);
final ghostVariableSpriteComponent = SpriteComponent(
sprite: spriteSheet.getSprite(1, 0),
size: spriteSize,
position: Vector2(50, 340),
);
add(vampireSpriteComponent);
add(ghostSpriteComponent);
add(ghostVariableSpriteComponent);
}
}