Skip to content

Commit c8ff752

Browse files
committed
(#3861) fix example , improve example
1 parent a2b7f93 commit c8ff752

File tree

6 files changed

+478
-284
lines changed

6 files changed

+478
-284
lines changed

packages/flame_texturepacker/README.md

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,89 @@ Import the plugin like this:
5656
Load the TextureAtlas passing the path of the sprite sheet atlas file:
5757

5858
```Dart
59-
final atlas = await atlasFromAssets('atlas_map.atlas');
59+
final atlas = await TexturePackerAtlas.load('atlas_map.atlas');
6060
```
6161

6262

63-
### Whitelist Images
63+
### Extension on Game
64+
65+
For convenience, there is also an extension on `Game` (and `FlameGame`)
66+
that allows you to load an atlas directly:
67+
68+
```dart
69+
class MyGame extends FlameGame {
70+
@override
71+
Future<void> onLoad() async {
72+
final atlas = await atlasFromAssets('atlas_map.atlas');
73+
// ...
74+
}
75+
}
76+
```
77+
78+
79+
### Loading from a Package
80+
81+
To load an atlas from another Flutter package, use the `package` parameter:
82+
83+
```Dart
84+
final atlas = await TexturePackerAtlas.load(
85+
'atlas_map.atlas',
86+
package: 'my_assets_package',
87+
);
88+
```
89+
90+
91+
### Paths and Prefixes
92+
93+
By default, `TexturePackerAtlas.load` looks for files in `assets/images/`. This is controlled by the
94+
`assetsPrefix` parameter, which defaults to `'images'`.
95+
96+
97+
#### 1. Default usage (relative to `assets/images/`)
98+
99+
```dart
100+
// Path: assets/images/hero.atlas
101+
final atlas = await TexturePackerAtlas.load('hero.atlas');
102+
```
103+
104+
105+
#### 2. Custom prefix (relative to `assets/`)
106+
107+
```dart
108+
// Path: assets/atlases/hero.atlas
109+
final atlas = await TexturePackerAtlas.load(
110+
'hero.atlas',
111+
assetsPrefix: 'atlases',
112+
);
113+
```
64114

115+
116+
#### 3. Full path (stripping standard prefix)
117+
If you provide a path that already includes the standard `assets/`
118+
or `images/` prefix, the library will automatically strip them to avoid duplication.
119+
This is particularly useful when working with full asset paths.
120+
121+
```dart
122+
// Path: assets/images/mega_explosions.atlas
123+
final atlas = await TexturePackerAtlas.load(
124+
'assets/images/mega_explosions.atlas',
125+
assetsPrefix: '',
126+
);
127+
```
128+
129+
130+
#### 4. Automatic Package Detection
131+
If you provide a path that starts with `packages/package_name/...`,
132+
the library will automatically detect the package name
133+
and adjust the internal loading logic.
134+
135+
```dart
136+
// Path: packages/my_assets_package/assets/images/heroes.atlas
137+
final atlas = await TexturePackerAtlas.load('packages/my_assets_package/assets/images/heroes.atlas');
138+
```
139+
140+
141+
### Whitelist Images
65142
This is optional, but recommended to avoid loading every sprite from your texture pack into memory.
66143
Use a list of relative paths to load only the Sprites you need into memory.
67144

@@ -76,12 +153,14 @@ final atlas = TexturePackerAtlas.fromAtlas(regions, whiteList: [
76153

77154

78155
### File Storage
79-
80156
If you are using file storage, grab your atlas file like this:
81157

82158
```Dart
83159
final documentsPath = (await getApplicationDocumentsDirectory()).path;
84-
final atlas = await atlasFromStorage('$documentsPath/atlas_map.atlas');
160+
final atlas = await TexturePackerAtlas.load(
161+
'$documentsPath/atlas_map.atlas',
162+
fromStorage: true,
163+
);
85164
```
86165

87166
Get a list of sprites ordered by their index, you can use the list to generate an animation:
@@ -96,6 +175,22 @@ final animation = SpriteAnimation.spriteList(
96175
);
97176
```
98177

178+
Or use the convenience method `getAnimation`:
179+
180+
```Dart
181+
final animation = atlas.getAnimation('robot_walk', stepTime: 0.1, loop: true);
182+
```
183+
184+
If your atlas contains multiple animations, load it once and reuse it:
185+
186+
```Dart
187+
final atlas = await TexturePackerAtlas.load('atlas_map.atlas');
188+
189+
final walkAnim = atlas.getAnimation('robot_walk');
190+
final runAnim = atlas.getAnimation('robot_run');
191+
final jumpAnim = atlas.getAnimation('robot_jump', loop: false);
192+
```
193+
99194
Get individual sprites by name:
100195

101196
```Dart
@@ -112,8 +207,8 @@ final idleSprite = atlas.findSpriteByName('robot_idle')!;
112207
| Allow Rotation | YES |
113208
| Multiple Pages | YES |
114209
| Use indices | YES |
115-
| Strip whitespace X | NO |
116-
| Strip whitespace Y | NO |
210+
| Strip whitespace X | YES |
211+
| Strip whitespace Y | YES |
117212

118213

119214
## Example
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
11
library flame_texturepacker;
22

3-
import 'package:flame/cache.dart';
4-
import 'package:flame/game.dart';
5-
import 'package:flame_texturepacker/src/texture_packer_atlas.dart';
6-
3+
export 'package:flame_texturepacker/src/extension_on_game.dart';
74
export 'package:flame_texturepacker/src/texture_packer_atlas.dart';
85
export 'package:flame_texturepacker/src/texture_packer_sprite.dart';
9-
10-
extension TexturepackerLoader on Game {
11-
/// Loads the specified pack file from assets
12-
/// Uses the parent directory of the pack file to find the page images.
13-
Future<TexturePackerAtlas> atlasFromAssets(
14-
String assetsPath, {
15-
Images? images,
16-
AssetsCache? assets,
17-
bool useOriginalSize = true,
18-
List<String> whiteList = const [],
19-
String assetsPrefix = 'images',
20-
}) async => TexturePackerAtlas.load(
21-
assetsPath,
22-
images: images ?? this.images,
23-
assets: assets ?? this.assets,
24-
useOriginalSize: useOriginalSize,
25-
whiteList: whiteList,
26-
assetsPrefix: assetsPrefix,
27-
);
28-
29-
/// Loads the specified pack file from storage
30-
/// Uses the parent directory of the pack file to find the page images.
31-
Future<TexturePackerAtlas> atlasFromStorage(
32-
String storagePath, {
33-
Images? images,
34-
bool useOriginalSize = true,
35-
List<String> whiteList = const [],
36-
}) async => TexturePackerAtlas.load(
37-
storagePath,
38-
fromStorage: true,
39-
images: images ?? this.images,
40-
useOriginalSize: useOriginalSize,
41-
whiteList: whiteList,
42-
);
43-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flame/cache.dart';
2+
import 'package:flame/game.dart';
3+
import 'package:flame_texturepacker/flame_texturepacker.dart';
4+
5+
extension TexturepackerLoader on Game {
6+
/// Loads the specified pack file from assets
7+
/// Uses the parent directory of the pack file to find the page images.
8+
Future<TexturePackerAtlas> atlasFromAssets(
9+
String assetsPath, {
10+
Images? images,
11+
AssetsCache? assets,
12+
bool useOriginalSize = true,
13+
List<String> whiteList = const [],
14+
String assetsPrefix = 'images',
15+
String? package,
16+
}) =>
17+
TexturePackerAtlas.load(
18+
assetsPath,
19+
images: images ?? this.images,
20+
assets: assets ?? this.assets,
21+
useOriginalSize: useOriginalSize,
22+
whiteList: whiteList,
23+
assetsPrefix: assetsPrefix,
24+
package: package,
25+
);
26+
27+
/// Loads the specified pack file from storage
28+
/// Uses the parent directory of the pack file to find the page images.
29+
Future<TexturePackerAtlas> atlasFromStorage(
30+
String storagePath, {
31+
Images? images,
32+
bool useOriginalSize = true,
33+
List<String> whiteList = const [],
34+
}) =>
35+
TexturePackerAtlas.load(
36+
storagePath,
37+
fromStorage: true,
38+
images: images ?? this.images,
39+
useOriginalSize: useOriginalSize,
40+
whiteList: whiteList,
41+
);
42+
}

0 commit comments

Comments
 (0)