Files
flame/packages/flame_texturepacker/example/lib/main.dart
Yayo Arellano 6c8190b721 refactor: Deprecate fromAtlas in favour of atlasFromAssets and atlasFromStorage (#3098)
Originally, the function was named `fromAtlas` because there was another
function named `fromJson` that loaded `.json` files. but since that one
is gone, and now every function is loading a `.atlas` it make sense to
have `fromAssets` and `fromStorage`

---------

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
2024-03-27 07:46:50 +00:00

73 lines
1.7 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_texturepacker/flame_texturepacker.dart';
import 'package:flutter/material.dart';
void main() {
final myGame = MyGame();
runApp(
GameWidget(
game: myGame,
),
);
}
class MyGame extends FlameGame {
@override
Future<void> onLoad() async {
super.onLoad();
// Load the atlasMap.
final atlas = await atlasFromAssets('atlas_map.atlas');
// Get a list of sprites ordered by their index
final walkingSprites = atlas.findSpritesByName('robot_walk');
// Create animation with the list of sprites
final walkingAnimation = SpriteAnimation.spriteList(
walkingSprites,
stepTime: 0.1,
);
// Get individual sprites by name
final jumpSprite = atlas.findSpriteByName('robot_jump')!;
final fallSprite = atlas.findSpriteByName('robot_fall')!;
final idleSprite = atlas.findSpriteByName('robot_idle')!;
// Get the list of all sprites in the sprite sheet
final allSprites = atlas.sprites; // ignore: unused_local_variable
add(
SpriteComponent(
sprite: jumpSprite,
position: Vector2(200, 100),
size: Vector2(72, 96),
),
);
add(
SpriteComponent(
sprite: fallSprite,
position: Vector2(300, 100),
size: Vector2(72, 96),
),
);
add(
SpriteComponent(
sprite: idleSprite,
position: Vector2(400, 100),
size: Vector2(72, 96),
),
);
add(
SpriteAnimationComponent(
animation: walkingAnimation,
position: Vector2(300, 200),
size: Vector2(72, 96),
),
);
}
}