Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Game Loop and Scenes

Working with Multiple Scenes in Phaser

Split a Phaser game into menu, play, HUD and pause scenes. Learn scene.start, launch, pause, resume, stop, passing data, the registry and scene-to-scene events.

Real games are made of several screens: a title menu, the game itself, a pause menu, a game-over screen, a settings page. Phaser's answer is one scene per screen. Scenes keep your code organized, let you reuse screens, and can even run at the same time, for example a HUD (score display) drawn on top of the gameplay.

Registering scenes

All scenes are listed in the game config. The first one starts automatically; the others wait:

JavaScript
new Phaser.Game({
  // ...
  scene: [Boot, Menu, Play, Hud, Pause, GameOver]
});

Each scene class calls super('Key') with a unique key. After that you control scenes by key through this.scene, the Scene Plugin every scene has.

The scene control methods

Call Effect
this.scene.start('Key', data) Stop this scene and start another. The usual "go to the next screen".
this.scene.launch('Key', data) Start another scene in parallel; this scene keeps running.
this.scene.pause('Key') / resume('Key') Freeze or unfreeze a scene's updates (it stays visible).
this.scene.sleep('Key') / wake('Key') Hide and freeze; objects are kept for a fast return.
this.scene.stop('Key') Shut a scene down completely.
this.scene.restart(data) Stop and start the current scene again.
this.scene.switch('Key') Sleep this scene and start/wake another.
this.scene.run('Key') Start it if stopped, resume it if paused, wake it if asleep.
this.scene.bringToTop('Key'), sendToBack, moveAbove Change the draw order of scenes.
this.scene.get('Key') Get the scene object so you can read its properties.

The difference between start and launch is the one to remember: start replaces, launch adds.

flowchart LR
  M["Menu"] -->|"start('Play')"| P["Play"]
  P -->|"launch('Hud')"| H["Hud (runs on top)"]
  P -->|"pause + launch('Pause')"| Q["Pause overlay"]
  P -->|"start('GameOver', data)"| G["GameOver"]

Play launches a HUD at the same time; start('GameOver') replaces Play.

A menu and a game

The first example is a classic flow. The Menu scene waits for a click (and starts the game by itself after a moment so the log shows the whole route), passes the player's chosen name to Play with start, and Play sends the final score on to GameOver.

Menu, play and game-over scenes

Look at how data moves: start('Play', {...}) delivers the object to Play.init(data). The same object also arrives in create(data), so use whichever fits. Each scene keeps only what it needs, and the object you pass is the only link between them.

Running scenes in parallel: a HUD

Many games draw a heads-up display (score, lives) in a separate scene on top of gameplay. The advantage: the HUD does not move with the gameplay camera, it can be restyled without touching game code, and it can be reused in every level. You start it with launch.

The two scenes then need to talk. Three good ways:

  1. Scene events: one scene emits on its own this.events; the other gets the scene with this.scene.get('Play') and listens.
  2. Game-wide registry (this.registry): a key-value store shared by all scenes that fires change events. Perfect for score, lives and settings.
  3. Direct property access: this.scene.get('Play').score. It works, but couples the scenes tightly; avoid it for anything that matters.

The next demo uses the registry. Play writes the score with this.registry.set('score', n). Hud listens with registry.events.on('changedata-score', ...). Neither scene holds a reference to the other. A pause overlay (the third scene) pauses Play and resumes it on click. Press the pause button in the HUD or click in the paused screen.

Play, HUD and pause overlay

Things worth noticing:

  • Hud is listed in the config but never starts by itself, because only the first scene does. Play launches it.
  • scene.pause('Play') freezes Play's update, timers, tweens and physics, so the score stops. The ball stays drawn, because a paused scene still renders.
  • The overlay scene is added on top because later scenes in the list are drawn later. You can change the order with this.scene.bringToTop('Pause').
  • On shutdown the HUD removes its registry listener. The registry belongs to the whole game, so listeners on it outlive scenes: clean them up.

Passing data: which tool when?

Need Use
Hand starting values to the next scene scene.start('Key', data)
Share state that every scene reads (score, lives, volume) this.registry
Let scenes announce something ("level-complete") this.events.emit and this.scene.get(...).events.on
Keep a rarely changing global an ES module that exports an object
Survive a page reload localStorage (see Packaging and Publishing)

Scene transitions

Instead of switching instantly you can animate between scenes. A simple fade uses the camera:

JavaScript
this.cameras.main.fadeOut(400, 0, 0, 0);
this.cameras.main.once('camerafadeoutcomplete', () => this.scene.start('Play'));

and in the next scene's create: this.cameras.main.fadeIn(400). Phaser also provides this.scene.transition({ target: 'Play', duration: 600, ... }) for more control, which lets both scenes run during the transition.

Common mistakes

  • Using start when you wanted launch. The current scene disappears, including the game you wanted to keep behind a pause menu.
  • Calling this.scene.start('Play') in a loop or from update. Each call restarts the scene. Guard it with a flag.
  • Forgetting to stop an overlay. Launching Pause twice stacks two overlays; stop it when done.
  • Leaking listeners on the registry or the game event emitter. Remove them on shutdown.
  • Holding a reference to a destroyed scene object. After stop the scene's game objects no longer exist; ask again with this.scene.get('Key') and rebuild in create.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between scene.start('B') and scene.launch('B')?

    start replaces the scene that called it. launch adds another scene that runs at the same time.
  2. 2. How do you hand the score to a GameOver scene?

    The second argument of start (and launch) is delivered to the target scene's init and create methods.
  3. 3. A pause menu should freeze the game but keep it visible. Which calls do that?

    pause freezes updates but the scene still renders; launching the Pause scene draws the overlay on top.
  4. 4. Which Phaser feature is a good place for score, lives and settings shared by all scenes?

    The registry is a game-wide data manager that emits change events, so scenes stay decoupled.
  5. 5. Why should a scene remove its registry listeners in shutdown?

    Only the scene's own systems are cleaned automatically. A listener on a game-wide emitter would keep firing after the scene has gone.

Summary

  • One scene per screen keeps a game organized; list scenes in the config and control them by key.
  • start replaces the current scene, launch runs another in parallel, pause/resume, sleep/wake and stop manage the rest.
  • Pass starting values with the data argument; share live state with the registry or scene events.
  • A HUD and a pause overlay are separate scenes running on top of the play scene.
  • Clean up game-wide listeners in shutdown and guard scene changes so they happen once.

Next: Shapes, Text and Graphics