flutterdartflame

How to Handle Texture Atlases (Image) in Flutter Flame


I'm trying to figure out how to do something similar to Love2D's Quad in Flame. Specifically, I want to load a texture atlas made from a PNG file.

For example, I'd like to work with the files in the Tilesheet folder of the following asset pack: https://kenney.nl/assets/playing-cards-pack (All the images are packed into a single file.)

Interestingly, the official Flame documentation mentions handling .fa files, but this is not what I'm looking for: https://docs.flame-engine.org/latest/bridge_packages/flame_fire_atlas/fire_atlas.html

I plan to treat the loaded images as SpriteComponent objects (if that's possible). If it's not possible, I can use the individual images provided as an alternative.


For reference:


Solution

  • There is a bridge package called flame_kenney_xml that allows you to easily load spritesheets from the kenney packs that have an xml definition.

    Example:

    final spritesheet = await XmlSpriteSheet.load(
      image: 'spritesheet.png',
      xml: 'spritesheet.xml`,
    );
    

    But if they don't have a definition for where the sprites are located in the image, they are most likely organized by rows and columns, and then you can just use the built-in Spritesheet class.

    Example:

        final image = await images.load('cardsLarge_tilemap.png');
        final sheet = SpriteSheet.fromColumnsAndRows(
          image: image,
          columns: 14,
          rows: 4,
        );