Manual Ship

On this page
  1. Export
  2. Play it
  3. Several sprites
  4. Pitfalls
  5. Next

Phaser

Export a sprite as a Phaser 3 texture atlas plus animation JSON, with each frame's origin at the feet.

The phaser format writes what Phaser 3 loads in two lines: a texture atlas whose frames carry their pivot at the feet, and an animation file for the Animation Manager with every animation's speed and loop setting.

The starter hero walking in a Phaser 3 scene at 2x
hero_phaser_anims.json playing in Phaser 3

Export#

Make a starter project to follow along, then export its hero:

bash
mode8 init my-game --template platformer
cd my-game
terminal
$ mode8 export assets/hero/frames --out-dir web/assets --name hero --formats phaser --animations assets/hero/master.m8.json
✓ Exported hero (15 frames) → web/assets/
  phaser · 4 files
  animations attack, hurt, idle, jump, walk
  palette nes_master_gothic from mode8.toml

Next:
  mode8 verify --assets-dir web/assets

Next to the atlas PNG you get:

  • hero_phaser_atlas.json, a "JSON hash" texture atlas in which each frame has an anchor at the feet. Phaser turns it into the frame's pivot, so a sprite stands on its feet instead of on its center.
  • hero_phaser_anims.json, the animations in Phaser's Animation Manager format: one key per animation, named <name>_<animation>, its frame rate, and repeat: -1 for a loop or 0 for a one-shot.
terminal
$ python3 -c "import json; print(json.load(open('web/assets/hero_phaser_anims.json'))['anims'][0])"
{'key': 'hero_attack', 'type': 'frame', 'frames': [{'key': 'hero', 'frame': 'windup', 'duration': 125}, {'key': 'hero', 'frame': 'attack'}, {'key': 'hero', 'frame': 'recover', 'duration': 125}], 'frameRate': 8, 'repeat': 0}

The plain hero_atlas.json is written too, unchanged, for tools that read the shared atlas layout.

Play it#

js
class Play extends Phaser.Scene {
  preload() {
    this.load.atlas("hero", "assets/hero_atlas.png", "assets/hero_phaser_atlas.json");
    this.load.animation("hero_anims", "assets/hero_phaser_anims.json");
  }

  create() {
    this.add.sprite(240, 220, "hero").setScale(2).play("hero_walk");
  }
}

new Phaser.Game({ type: Phaser.AUTO, width: 480, height: 270, pixelArt: true, scene: Play });

The texture key is the export's --name, so it must match the first argument of load.atlas. pixelArt: true turns on nearest filtering and pixel rounding for the whole game. A one-shot animation such as hero_attack plays once and stops on its last frame; listen for the sprite's animationcomplete event to go back to hero_idle.

Several sprites#

Each sprite gets its own --name, its own texture key and its own animation keys, so a whole cast shares one folder:

terminal
$ mode8 export assets/slime/master.png --out-dir web/assets --name slime --formats phaser
✓ Exported slime (1 frame) → web/assets/
  phaser · 4 files
  animations master
  palette nes_master_gothic from mode8.toml

Next:
  mode8 verify --assets-dir web/assets

Pitfalls#

  • The sprite stands on its center. Load hero_phaser_atlas.json, not hero_atlas.json: only the Phaser file carries the pivots.
  • "Missing animation" means the key differs: animations are called <name>_<animation>, such as hero_walk.
  • Blurry sprites. Set pixelArt: true in the game config, and scale by whole numbers.

Next#