I'm developing a game using the Flutter Flame engine and have packaged the game to be used in another Flutter app. However, I'm encountering an issue where the game, when used as a package dependency in the host app, cannot find its image assets.
My Flame game has its assets located in assets/images/ and they are properly listed in the pubspec.yaml
of the game package. It loads the image like this:
spriteImage = await Flame.images.load('abc.png');
I've included the game package as a dependency in my host app's pubspec.yaml
. Despite the assets being accessible within the game package, when I run the host app, it stops with exception:
Unable to load asset: "assets/images/abc.png"`
How can I ensure that my Flutter Flame game package can correctly load its assets when used as a dependency in another Flutter app?
Is there in Flame something like the package
parameter in the Flutter:
Image.asset('assets/images/abc.png', package: 'my_game')
Found it out. The answer from @Hamed pointed me in the right direction. I only had to adjust the prefix of the flame image cache:
class MyGame extends FlameGame with ... {
@override
Future<void> onLoad() async {
images.prefix = 'packages/ma_package/assets/images/'; // <- added this line
...
}
}