Skip to content

Instantly share code, notes, and snippets.

@samme
Last active August 2, 2023 16:26
Show Gist options
  • Save samme/01a33324a427f626254c1a4da7f9b6a3 to your computer and use it in GitHub Desktop.
Save samme/01a33324a427f626254c1a4da7f9b6a3 to your computer and use it in GitHub Desktop.
Phaser 3 Scenes Summary

Scene control

Calls without a key argument

These affect the calling scene only.

Example:

this.scene.start()
method calling scene notes
pause pause
remove destroy Queued during scene processing
restart shutdown?, start Shutdown first only if RUNNING or PAUSED.
resume resume
sleep sleep
start shutdown?, start Shutdown first only if RUNNING or PAUSED.
stop shutdown
wake wake

remove() is queued while the Scene Manager is updating or rendering, or run immediately otherwise.

All other operations are queued always.

start() and restart() are identical when called without a key argument.

Calls with a key argument

These affect the target scene and may affect the calling scene.

Example:

this.scene.start('target')
method calling scene target scene notes
launch shutdown?, start Target scene is shutdown first only if RUNNING or PAUSED. If the calling scene is also the target, nothing happens
pause pause
remove destroy Queued during scene processing
resume resume
run shutdown?, start / wake / resume Target scene is shutdown first only if RUNNING
sleep sleep
start shutdown shutdown?, start Target scene is shutdown first only if RUNNING or PAUSED
stop shutdown
switch sleep shutdown?, start / wake Target scene is shutdown first only if RUNNING or PAUSED
wake wake

remove() is queued while the Scene Manager is updating or rendering, or run immediately otherwise.

All other operations are queued always.

restart() never takes a key argument. Use launch('target') instead.

Scene Systems

Scene operations change a scene's state and trigger certain events.

Within a scene, you can listen to these events from this.events.

active means a scene is updating its objects and plugins.

visible means a scene is rendering its objects and plugins.

method status identity active visible events
init INIT boot
start START yes yes start, ready
pause PAUSED isPaused() no pause
resume RUNNING isActive() yes resume
sleep SLEEPING isSleeping() no no sleep
wake RUNNING isActive() yes yes wake
shutdown SHUTDOWN no no shutdown
destroy DESTROYED no no destroy

Scene Flow

Table shows: SCENE STATUS, scene events, scene methods

INIT START LOADING CREATING RUNNING PAUSED SLEEPING SHUTDOWN DESTROYED
boot
start (resume/wake) pause sleep shutdown destroy
ready
init
create
preupdate preupdate
update update
update
postupdate postupdate
prerender prerender prerender
render render render
  • resume is emitted only for PAUSED → RUNNING.
  • wake is emitted only for SLEEPING → RUNNING.

References

@lozzajp
Copy link

lozzajp commented May 28, 2020

Hey Samme, feel free to chuck this table in I feel it adds some value to people trying to pass data around between scenes.

ScenePlugin methods usage with data

method Accepts Data Event emitted with data Methods called with data
pause Yes pause
resume Yes resume
sleep Yes sleep
wake Yes wake
stop Yes shutdown
start Yes ready init, create
launch Yes ready init, create
restart Yes ready init, create
add Yes ready init, create
run Yes wake or resume or ready init, create*
switch No

Start calls them in the order ready, init, create but will only call init and create if they are defined.

Launch will call Start but on a different scene, and go through the same flow.

Restart will call stop on the scene without data, and call start again with data (and once again go through ready, init, create).

Add will follow the same process as Start, but only if the scene is set to autostart or active in it's config, or will call them once the scene is started.

Run will emit an event depending on the initial state of the scene. If scene is asleep it will wake it and emit wake with data, or if paused it will resume it and emit resume with data. *Otherwise if it has to start the scene, it will go through the start process with the data and emit ready then call init, create if they are defined.

Sources:
Same as you
and:
https://github.com/photonstorm/phaser/blob/master/src/scene/typedefs/SceneInitCallback.js
https://github.com/photonstorm/phaser/blob/master/src/scene/typedefs/SceneCreateCallback.js

@samme
Copy link
Author

samme commented Jul 23, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment