Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Game Loop and Scenes

Phaser Scenes and the Scene Lifecycle

Learn how a Phaser scene starts and ends: init, preload, create and update, the scene keys, scene events, shutdown cleanup and how to restart a scene safely.

A scene is the unit you spend most of your time in. It owns a part of the game: its objects, its input, its timers and its logic. A scene is not just a function that runs once. It has a lifecycle: Phaser calls specific methods in a fixed order, and the scene can later be paused, stopped or restarted. If you know the order, you always know where to put code.

The four methods

A scene class can define up to four special methods. Phaser calls whichever exist:

flowchart LR
  A["init(data)"] --> B["preload()"]
  B --> C["create(data)"]
  C --> D["update(time, delta)"]
  D -.->|"every frame"| D

init, preload and create run once; update repeats until the scene stops.

Method Runs Use it for
init(data) Once, first Reset variables, read data passed from another scene
preload() Once, after init Queue files to load (images, audio, JSON)
create(data) Once, after loading finishes Build the world: add objects, set up input and physics
update(time, delta) Every frame Move things, check conditions

Every one of them is optional. A scene with only create is perfectly valid, which is what you used so far.

The loader waits: Phaser does not call create until every file queued in preload has finished loading. That guarantee is why you can use textures safely in create.

A scene needs a key

Each scene has a unique key, a string used to refer to it. You set it in the constructor with super('KeyName'):

JavaScript
class Menu extends Phaser.Scene {
  constructor() {
    super('Menu');
  }
}

If you pass nothing, Phaser uses a default key (default for the first scene), which is fine for a one-scene game but confusing later. Always give scenes a key; you need it for this.scene.start('Menu').

You can also pass a configuration object instead of a string, for example to give the scene its own physics or plugins:

JavaScript
super({ key: 'Play', physics: { arcade: { gravity: { y: 400 } } } });

Watching the lifecycle

The best way to understand the order is to log it. The scene below prints each stage and counts frames. After three frames it asks the scene manager to stop itself. Notice that update receives two arguments: time is the total time since the game started and delta is the milliseconds since the previous frame.

Lifecycle logger

You should see the numbers in order, one init, one preload, one create, three update calls and finally the shutdown event. Note that the constructor runs even earlier, when the game is created, so it is not the place to touch game objects, because the scene's systems do not exist yet.

Where does each kind of code belong?

A useful rule of thumb:

  • Constructor: only set the key (and, rarely, scene-level config).
  • init: reset state variables (score, lives, flags) and read data. Because init runs every time the scene starts, resetting here makes restarts work.
  • preload: only this.load.* calls. No game objects yet.
  • create: create objects, colliders, input handlers, timers. Everything that must exist before the first frame.
  • update: small, cheap per-frame logic. Do not create objects here without a good reason; allocating every frame causes lag spikes.

A frequent bug is putting this.score = 0 only in the constructor. The constructor runs once for the whole game, so after scene.restart() the old score survives. Reset in init instead.

Passing data into a scene

this.scene.start('Key', data) hands an object to the next scene's init(data) and create(data). This is the clean way to say "start the level with this number" or "show this score on the game-over screen":

JavaScript
// in the Play scene, when the player dies
this.scene.start('GameOver', { score: this.score, level: 3 });

// in the GameOver scene
init(data) {
  this.finalScore = data.score ?? 0;   // ?? gives a default if no data was passed
}

Scene events and cleanup

Every scene has an event emitter at this.events. The important events:

Event When
create After create() finished
update Every frame, before update() is called
pause, resume The scene was paused or resumed
sleep, wake The scene was put to sleep or woken (kept in memory)
shutdown The scene is stopping; it may start again later
destroy The scene is being removed for good

Shutdown is your cleanup hook. Scene systems (input, timers, tweens) clean themselves, but things you attach elsewhere do not. If you add a listener to the global game events or to window, remove it on shutdown; otherwise it stays alive and fires for a scene that is no longer running. The constant Phaser.Scenes.Events.SHUTDOWN equals the string 'shutdown'.

The example below restarts a scene three times, passes a run number through data and shows state resetting correctly. It restarts on its own after 1.2 seconds, so just watch the colors and the console.

Restart with data and cleanup

Every run starts from zero because init resets the score. Without that line the scores would add up across runs.

Pausing, sleeping and stopping

The scene manager (this.scene) offers several controls; they differ in what they keep:

Call Update runs? Rendered? Objects kept?
this.scene.pause() No Yes Yes
this.scene.sleep() No No Yes (fast to wake)
this.scene.stop() No No No: shut down
this.scene.restart() stop, then start again Rebuilt from create

Pause is what a pause menu uses: the game freezes but stays visible behind the menu. Multiple Scenes builds exactly that.

Common mistakes

  • Using this.add in the constructor. The scene is not ready yet; move the code to create.
  • Forgetting to reset state in init. Restarts keep the old values.
  • Creating objects in update every frame. Create once in create and update properties instead.
  • Two scenes with the same key. The second one is ignored, which is very confusing; keep keys unique.
  • Assuming create runs before assets load. It runs after, but only for files queued in preload of that scene (or already loaded earlier).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In which order does Phaser call a scene's methods?

    init runs first, then preload (loading), then create once everything has loaded, then update repeats every frame.
  2. 2. Where should you reset the score so that scene.restart() starts clean?

    The constructor runs once per game, while init runs each time the scene starts, so it is the right place to reset state.
  3. 3. What are the two parameters of update(time, delta)?

    time is the total running time in milliseconds; delta is the time since the previous frame, which you use to scale movement.
  4. 4. You added a listener to window inside a scene. When should you remove it?

    Listeners attached outside the scene are not cleaned automatically. Remove them in a shutdown handler.

Summary

  • A scene runs init(data), preload(), create(data) once and update(time, delta) every frame; all are optional.
  • Give every scene a unique key with super('Key') and keep real work out of the constructor.
  • Reset state in init; build objects in create; keep update cheap.
  • this.scene.start('Key', data) passes data to the next scene; restart, pause, sleep and stop control the scene.
  • Use the shutdown event to clean up anything you attached outside the scene.

Next: The Game Loop and Delta Time