Skip to content

πŸ”₯ Flame Integration

The ssplayer_flame package adds SpriteStudioComponent β€” a PositionComponent that plays a SpriteStudio animation inside a FlameGame. It owns an externally-clocked SpriteStudioPlayer (no Ticker): Flame's game loop drives time (update(dt) β†’ advance, render β†’ paint). It is a separate package so the core never pulls in flame, and it re-exports the whole ssplayer_flutter API.

The live Flame demo is examples/flame built for the web: one SpriteStudioComponent playing Ringo, tap to play / pause.

Add the dependency

flutter pub add flame ssplayer_flame

ssplayer_flutter and ssruntime_dart come with ssplayer_flame; the rest of the setup is on Installation.

Add a component to your game

class RingoGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    add(
      SpriteStudioComponent.asset(
        // Sibling reference textures resolve from the key's own folder
        // (embedded packs work the same way).
        'assets/spritestudio/Ringo/Ringo.ssab',
        animation: 'wait1',
        position: Vector2(200, 200),
        anchor: Anchor.center,
        loopCount: -1,
      ),
    );
  }
}

SpriteStudioComponent.asset(path, …) loads the .ssab from the asset bundle in onLoad and resolves sibling reference textures from the key's own folder. To play bytes you already hold, use the default constructor: SpriteStudioComponent(ssab: myBytes, baseAssetDir: 'assets/spritestudio/Ringo'). Bytes carry no asset key, so baseAssetDir names the asset folder that holds the sibling reference textures; an embedded pack needs none.

Key constructor options

Option Default Description
animation first in pack Animation to select on load
loopCount -1 Loop count (-1 forever, n plays n times)
speed 1.0 Playback speed multiplier
subFrame true Sub-frame interpolation
autoStart true Play as soon as the resource loads (set false to drive it yourself)
onEvents / onAnimationStarted / onAnimationFinished / onAnimationLooped null Forwarded to the player's callbacks (see Playback & Events)

position, scale, angle, anchor and priority work as on any PositionComponent. size is not a scale: it is the box the animation is drawn in. The animation's origin lands on the middle of the box (size / 2), or on position while size is the default zero. On the flutter_gpu backend the box is also the frame, and anything outside it is cropped, so an explicit size must hold the artwork with room on every side of the origin (twice the authored canvas per axis is a safe start). anchor only moves the box, so Anchor.center has an effect once you give a size.

A button

SpriteStudioButtonComponent is a SpriteStudioComponent that is a button: one animation shows its states by the rule the Flutter widgets follow β€” labels named after the states, their fallbacks, and clips to use other animations or labels (see Buttons & UI States).

add(
  SpriteStudioButtonComponent.asset(
    'assets/ui/start.ssab',
    hitParts: const ['face'],
    onReleased: startGame,
    size: Vector2(240, 120),
    position: Vector2(200, 300),
    anchor: Anchor.center,
  ),
);
State Set by
hover a mouse over the button
pressed a tap held on it
disabled isDisabled
focused isFocused. Flame has no focus of its own, so a menu driven by a gamepad or the keyboard marks the button it has selected
Option Default Description
onPressed / onReleased / onCancelled null A tap going down, coming up, and cancelled, as on Flame's ButtonComponent. A disabled button fires none
hitParts null The parts a pointer counts on, by their outlines as drawn, within the box. Null is the whole box
hitOnArtwork false With no hitParts, count a pointer on any part rather than on the whole box
clips none Per-state replacements for the labels
isDisabled / isFocused / reducedMotion false Also properties, to change at any time. Flame reads no platform setting, so reducedMotion is the game's own choice

The animation is drawn about the middle of the box, so give the button a size that holds its artwork. The button takes its player's playback over, which is why it has no loopCount, speed, autoStart, onEvents or onAnimation… arguments. examples/flame/lib/button_check.dart is an app with three of them, one for each way of taking a pointer. It runs from a clone of the repository (flutter run -t lib/button_check.dart, set up as in the example's README).

Controlling playback

After onLoad, reach the whole SpriteStudioPlayer β€” transport, events, part overrides β€” through component.player. It is null until the load finishes:

component.player?..setAnimation('attack')..play();

The clock is Flame's, so there is no Ticker β€” transport state still lives on the player.

A load that fails (a wrong asset key, a malformed .ssab) does not throw: the component draws nothing, player stays null, the error is in component.loadError, and it goes to FlutterError.onError.

Releasing it

A component releases its native runtime context and decoded textures when it is removed from the tree, and loads them again if you add it back. Tearing down a GameWidget (a popped route, a rebuilt widget) does not remove the components, so release them from the game, and keep the game in a State field rather than creating it in build:

class RingoGame extends FlameGame {
  // …onLoad as above…
  @override
  void onDispose() {
    dispose(); // removes the children, which releases each component's player
    super.onDispose();
  }
}

The RingoGame above is the whole game. examples/flame/ is the same thing as an app you can run from a clone of the repository.