Events - samme/phaser3-faq GitHub Wiki

Phaser 3 uses a slightly modified eventemitter3, as Phaser.Events.EventEmitter.

  • Event names are strings
  • The emitter chooses a default context (this value), but listeners can override this
  • The emitter chooses the callback arguments
  • on() is the same as addListener()
  • off() is the same as removeListener()
  • off() with no arguments is the same as removeAllListeners()
  • off() can match event name; event name and listener; or event name, listener, and context; but no other combination

Some Phaser classes (e.g., GameObject) extend Phaser.Events.EventEmitter directly, and some (e.g., Game, Scene) hold it on events.

You can emit and listen to your own events on these objects, but don't use any of Phaser's event names (e.g., "update"), and never remove listeners that aren't yours. Be careful of removing more than you meant to with off().

// NO (removes all listeners on all events)
this.game.events.off();

// NO (removes all listeners to `step`)
this.game.events.off('step');

All of Phaser's events (names, default context, arguments) are defined in the API.

Remember that event emitters just hook functions up to other functions. The emitter doesn't know or care what the listener function is. It's up to you to remove listeners when appropriate.