flame icon indicating copy to clipboard operation
flame copied to clipboard

AssetSource SpriteAnimationWidget.asset SpriteWidget.asset should support package args

Open cruvie opened this issue 9 months ago • 2 comments

What could be improved

like flutter

 Image.asset(
              'assets/images/banner.png',
              package: 'demo',
            ),

we can specify package for asset so that we can make this pkg (include Image.asset )as a third-part pkg, which can be used as dependency for other project.

Why should this be improved

flame asset related function support package args

now when we use a pkg include such as AssetSource we will get error like this

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unable to load asset: "assets/images/scenery/cliffs.png".
The asset does not exist or has empty data.
#0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:333:13)
<asynchronous suspension>
#1      Images._fetchToMemory (package:flame/src/cache/images.dart:180:18)
<asynchronous suspension>
#2      ParallaxImage.load (package:flame/src/parallax.dart:149:7)
<asynchronous suspension>
#3      Parallax.load.<anonymous closure> (package:flame/src/parallax.dart:518:26)
<asynchronous suspension>

Risks

No response

More information

No response

Other

  • [ ] Are you interested in working on a PR for this?

cruvie avatar Apr 12 '25 14:04 cruvie

I agree, these classes should be more in line with how Flutter does it. We would have to start supporting the package argument in the Images class in some way.

As a workaround, you can either set the prefix of the static Flame.images cache to include your package:

Flame.images.prefix = 'packages/<your package>/.../`;

Or you could pass in a custom Images cache to the instance that you are creating:

SpriteWidget.asset(
  path: ...,
  images: Images(prefix: 'packages/<your package>/.../`),
);

The key here is to prefix the prefix with packages/, because that is the same thing as Flutter does under the hood when specifying the package argument on Image.asset.

spydon avatar Apr 13 '25 13:04 spydon

@spydon thanks, the workaround works for me and looking forward to this feature

cruvie avatar Apr 13 '25 14:04 cruvie