Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Audio and UI

HUDs, Menus and Pause Screens

Build a HUD and menus in Phaser using separate UI scenes: score and health bar, a title menu, pause and resume, and data passing between scenes.

The HUD (heads-up display) is everything the player reads while playing: score, health, ammo, a timer, a minimap. Around it sit the menus: a title screen, a pause screen, a game-over screen. All of these are user interface, and they have different needs from the game world. They must not scroll with the camera, must stay readable when the world shakes or zooms, and must keep working when the game is paused.

The cleanest way to get all of that in Phaser is to give the UI its own scene. Scenes can run in parallel, drawn on top of each other, and each one has its own cameras, its own input and its own update loop. In this lesson you build a HUD scene that listens to the game scene, then a title menu and a pause screen that control scene states.

Why a separate UI scene

You could put score text in the game scene and call setScrollFactor(0) on it. That works for a small game, but it gets awkward as the UI grows:

  • A shake or zoom on the game camera also moves the HUD, unless you add a second camera (see Camera Effects and Multiple Cameras).
  • If the game scene pauses, restarts or is replaced by the next level, the HUD would have to be rebuilt each time.
  • UI code mixes with gameplay code and becomes hard to read.

A UI scene solves all three. It is launched once and stays on top, and it can show information from whichever level is currently running.

flowchart LR
  subgraph Parallel scenes
    G["Game scene<br/>(world, player, enemies)"]
    H["HUD scene<br/>(score, health bar)"]
  end
  G -- "events: score, health" --> H
  H -- "draws on top" --> Screen
  G --> Screen

The game scene emits events; the HUD scene listens and redraws. They never touch each other's objects.

Running scenes in parallel

Phaser's scene manager has these key calls (the scene key is the string you gave the scene in its constructor):

Call What happens
this.scene.start('Key') Stops the current scene and starts another one
this.scene.launch('Key') Starts another scene in parallel, on top of the current one
this.scene.pause('Key') / resume('Key') Freezes or unfreezes updating (the scene stays visible)
this.scene.sleep('Key') / wake('Key') Like pause, but also hides the scene
this.scene.stop('Key') Shuts the scene down
this.scene.bringToTop('Key') Changes the draw order
this.scene.get('Key') Returns the scene object, so you can use its events

Paused scenes stop their game time too: tweens, timers and physics freeze and continue where they stopped when resumed. That is exactly what a pause menu needs.

A HUD scene

In this example the game scene drops coins and bombs. Click a yellow coin to score. Clicking a red bomb costs health, and a coin that reaches the floor costs health too. The game scene knows nothing about how the HUD looks: it only announces score and health events. The HUD scene subscribes to them and draws the bar.

Game scene plus HUD scene

Read the structure carefully:

  • scene: [Game, HUD] registers both scenes. Only the first starts automatically; launch('HUD') starts the second.
  • The game scene owns the state (score, health) and emits events when it changes. The HUD owns the drawing. If you later redesign the HUD, the game code does not change.
  • The HUD removes its listeners on shutdown. Without this, restarting the game would leave dead callbacks that fire again and again (a classic memory leak and source of strange bugs).
  • The health bar is two rectangles: a dark frame and a fill whose width is tweened, which gives a smooth drain.

Menus follow the same idea with scene switches. A title scene starts the game with scene.start('Game'). A pause screen is launched over the running game after pausing it:

JavaScript
// in the game scene, when P is pressed
this.scene.pause();                 // freeze the game
this.scene.launch('Pause');         // draw the pause menu on top
JavaScript
// in the pause scene
resumeButton.on('pointerup', () => {
  this.scene.stop();                // close the pause menu
  this.scene.resume('Game');        // unfreeze the game
});
Title screen, game and pause menu

Start the game, wait a few seconds and press Pause. The ball freezes in mid-air and the timer stops, because paused scenes stop their tweens and their clock. Resume continues exactly where it stopped. The dark overlay rectangle is interactive so that clicks do not leak through to anything underneath.

Notice const { width, height } = this.scale;: instead of typing 240, the menu is laid out from the actual game size. When you later change the resolution or use a responsive scale mode (see Scale Manager and Mobile), the layout still works.

Passing data between scenes

Two more tools make menus useful. scene.start('Play', { level: 3 }) passes an object that arrives in the target scene's init(data) method, which runs before create(). And the global registry (this.registry.set('score', 10)) stores values that every scene can read; the lesson on events, registry and saving covers both in depth.

JavaScript
class Play extends Phaser.Scene {
  init(data) {
    this.level = data.level ?? 1;      // a default when the menu passes nothing
  }
}

UI design habits that pay off

  • Layout from the screen size, not magic numbers. Use this.scale.width / 2 and percentages so menus survive a resolution change.
  • Feedback on every control. A hover color, a pressed scale and a sound make a button feel real.
  • Large touch targets (at least 44 pixels) and a pause button visible on mobile, where there is no P key.
  • Pause when the tab is hidden. Listen for this.game.events.on('hidden', ...) and open the pause menu.
  • Keep the HUD light. Show only what the player needs right now; hide the rest in the pause menu.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does this.scene.launch('HUD') do compared with this.scene.start('HUD')?

    launch adds another running scene on top of the current one, while start shuts down the calling scene and switches to the new one.
  2. 2. Why should the HUD scene remove its event listeners on shutdown?

    The game scene's emitter outlives the HUD scene. Stale callbacks would keep firing and touch destroyed objects.
  3. 3. While a scene is paused with scene.pause(), what happens to its tweens and timers?

    A paused scene stops its update loop and its clock, so tweens and time events freeze.
  4. 4. How do you hand a level number to a scene you are starting?

    The second argument of scene.start is delivered to the target scene's init method before create runs.

Summary

  • A UI scene running in parallel keeps the HUD separate from the game world, cameras and restarts.
  • The game scene owns state and emits events; the HUD listens, draws and unsubscribes on shutdown.
  • launch, start, pause, resume, sleep and stop control which scenes run; a paused scene freezes its tweens and timers.
  • A pause menu is a scene launched over a paused game, with an input-blocking overlay.
  • Lay out UI from this.scale values and pass data with scene.start(key, data).

Next: Scene Architecture