flutter_gen icon indicating copy to clipboard operation
flutter_gen copied to clipboard

Flame integration

Open markohrastovec opened this issue 2 years ago • 2 comments

FlutterGen is wonderfull, but I do not know how to use it with Flame. Flame already assumes that images are in assets/images/. When loading an image image.load('filename.png') is used without the path. FlutterGen generates the whole path. I guess I could always remove assets/images/ from the string before using it, but it does not feel right. Flame also has assets/audio/. Will there be support for audio, too, sometime in the future?

markohrastovec avatar Dec 23 '21 18:12 markohrastovec

Thanks to you I finally understood about Flame. I'll develop the Flame integration again.

wasabeef avatar Jan 04 '22 05:01 wasabeef

@markohrastovec Just update the prefix like this.

// If your assets directory structure is like this.
/**
└── images
    └─── dir_name
        └── image_name.png
*/
// world
class MyWorld extends World with HasGameReference<MyGame> {
  @override
  Future<void> onLoad() async {
    // Here is the point.
    game.images.prefix = '';
    await game.images
        .loadAll(Assets.images.dirName.values.map((v) => v.keyName).toList());
    add(MyComponent());
  }
}
// component
class MyComponent extends SpriteComponent with HasGameReference<MyGame> {
  @override
  Future<void> onLoad() async {
    final image =
        game.images.fromCache(Assets.images.dirName.imageName.keyName);
    sprite = Sprite(image);
  }
}

howlwindy avatar Feb 18 '24 13:02 howlwindy

It seems no extra work is required in the package. The above comment already suggested a valid workaround.

AlexV525 avatar Feb 29 '24 05:02 AlexV525