FlutterIntermediate 3 min read

Flame sprites, assets and component lifecycle

Load a sprite before using it, separate world scale from asset pixels and keep interface overlays outside asset-loading logic.

On this page

A sprite is a rendered image or region of an image used by a game component. Asset pixel dimensions and the component’s world size are related by your design, not automatically identical.

Establish an asset convention

Follow the selected Flame version’s asset-path conventions and declare the files in pubspec.yaml. Load required resources before beginning gameplay so a missing image does not silently create an invisible target.

The fragment belongs in a FlameGame onLoad method after adding the engine package and importing its component types.

Worked example

This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.

@override
Future<void> onLoad() async {
  await super.onLoad();
  final spark = await SpriteComponent.load('spark.png',
    size: Vector2.all(32), anchor: Anchor.center);
  spark.position = Vector2(100, 120);
  await add(spark);
}

Read the result

The component has an explicit size and anchor. The asset must exist at the configured image path. For animation, define how frames are laid out and how playback relates to game state rather than advancing a frame for every arbitrary input event.

Check size, anchor and readiness independently

The source image supplies pixels. Component size supplies world dimensions. Anchor says which point those coordinates represent. A large source bitmap can therefore render as a small world object, and changing the anchor can move its apparent position without changing its coordinates.

Wait for required assets before declaring gameplay ready. If an essential target fails to load, show a recoverable loading failure instead of allowing an invisible entity to participate in the game.

Use the asset-free Flame starter as a baseline, then replace its shape with one sprite. This isolates asset and anchor problems from movement and boundary math.

Review an asset before adding it to the loading path

Record an asset’s source, usage rights, dimensions and animation-frame layout. For an AI-generated sprite, inspect transparency edges and consistency between frames instead of assuming the output is ready for an atlas. A tiny display size does not guarantee a small decoded texture.

Load required assets before starting the round, show a useful loading state and keep a failure message or fallback shape. Cache reusable assets through the engine’s supported mechanism rather than decoding the same file for every enemy. Verify case-sensitive filenames because a build can fail on a platform with different filesystem rules.

Test restart after a failed asset load and confirm that score and timers do not start invisibly behind the loading screen. The AI game-testing workflow includes asset/content review alongside rule checks and device behavior.

Practice and verification

Load a real image, then deliberately rename the file and inspect the failure. Restore it and change the component size without editing the bitmap. Keep a game loading state visible until required assets are ready.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.