fix: resolve image caching issue on flame_game template
BBarisKilic opened this issue · comments
Bulent Baris Kilic commented
Description
There is a bug on the generated flame_game template. The cached images are not accessible from the FlameGame
, therefore they are not "actually" cached and causing a runtime error.
Steps To Reproduce
- Go to
lib
>game
>entities
>unicorn
>unicorn.dart
. - The
onLoad
method of theUnicorn
class is currently like this:
@override
Future<void> onLoad() async {
final animation = await gameRef.loadSpriteAnimation(
Assets.images.unicornAnimation.path,
SpriteAnimationData.sequenced(
amount: 16,
stepTime: 0.1,
textureSize: Vector2.all(32),
loop: false,
),
);
await add(
_animationComponent = SpriteAnimationComponent(
animation: animation,
size: size,
),
);
resetAnimation();
}
- Modify the
onLoad
method as follows to produce the issue in an easy way:
@override
Future<void> onLoad() async {
final animation = SpriteAnimation.fromFrameData(
gameRef.images.fromCache(Assets.images.unicornAnimation.path),
SpriteAnimationData.sequenced(
amount: 16,
stepTime: 0.1,
textureSize: Vector2.all(32),
loop: false,
),
);
await add(
_animationComponent = SpriteAnimationComponent(
animation: animation,
size: size,
),
);
resetAnimation();
}
- Run the project and the error will be thrown as soon as the
Start
button gets clicked.
Expected Behavior
I would expect not to receive a runtime error that claims the image is not cached even though it is cached.
Additional Context
Error message: "Tried to access an image \"assets/images/unicorn_animation.png\" that does not exist in the cache. Make sure to load() an image before accessing it"