EventEmitter
EventEmitter is a Scene Systems plugin compatible wrapper around the eventemitter3 library, providing a full-featured event emitter used throughout Phaser for event-driven communication between game objects, scenes, and systems.
Every Scene has an instance of this class available via scene.events, and many Phaser objects extend or embed it to dispatch and receive events. It supports persistent listeners via on / addListener, one-time listeners that auto-remove after firing via once, and listener removal via off / removeListener.
You can also instantiate it directly when you need a standalone event bus within your own code.
Scope: static
Source: src/events/EventEmitter.js#L11
Since: 3.0.0
Public Methods
addListener
<instance> addListener(event, fn, [context])
Description:
Add a listener for a given event.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| event | string | symbol | No | The event name. | |
| fn | function | No | The listener function. | |
| context | * | Yes | "this" | The context to invoke the listener with. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L124
Since: 3.0.0
destroy
<instance> destroy()
Description:
Removes all listeners from this EventEmitter and prepares it for garbage collection. This method is called automatically when the parent object is destroyed and should not be called directly unless you are tearing down the emitter manually.
Source: src/events/EventEmitter.js#L53
Since: 3.0.0
emit
<instance> emit(event, [args])
Description:
Calls each of the listeners registered for a given event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | No | The event name. |
| args | * | Yes | Additional arguments that will be passed to the event handler. |
Returns: boolean - true if the event had listeners, else false.
Source: src/events/EventEmitter.js#L99
Since: 3.0.0
eventNames
<instance> eventNames()
Description:
Return an array listing the events for which the emitter has registered listeners.
Returns: Array.<(string | symbol)> -
Source: src/events/EventEmitter.js#L68
Since: 3.0.0
listenerCount
<instance> listenerCount(event)
Description:
Return the number of listeners listening to a given event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | No | The event name. |
Returns: number - The number of listeners.
Source: src/events/EventEmitter.js#L88
Since: 3.0.0
listeners
<instance> listeners(event)
Description:
Return the listeners registered for a given event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | No | The event name. |
Returns: Array.<function()> - The registered listeners.
Source: src/events/EventEmitter.js#L77
Since: 3.0.0
off
<instance> off(event, [fn], [context], [once])
Description:
Remove the listeners of a given event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | No | The event name. |
| fn | function | Yes | Only remove the listeners that match this function. |
| context | * | Yes | Only remove the listeners that have this context. |
| once | boolean | Yes | Only remove one-time listeners. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L165
Since: 3.0.0
on
<instance> on(event, fn, [context])
Description:
Add a listener for a given event.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| event | string | symbol | No | The event name. | |
| fn | function | No | The listener function. | |
| context | * | Yes | "this" | The context to invoke the listener with. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L111
Since: 3.0.0
once
<instance> once(event, fn, [context])
Description:
Add a one-time listener for a given event. The listener is automatically removed the first time the event is emitted, so it will never be called more than once.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| event | string | symbol | No | The event name. | |
| fn | function | No | The listener function. | |
| context | * | Yes | "this" | The context to invoke the listener with. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L137
Since: 3.0.0
removeAllListeners
<instance> removeAllListeners([event])
Description:
Remove all listeners, or those of the specified event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | Yes | The event name. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L179
Since: 3.0.0
removeListener
<instance> removeListener(event, [fn], [context], [once])
Description:
Remove the listeners of a given event.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| event | string | symbol | No | The event name. |
| fn | function | Yes | Only remove the listeners that match this function. |
| context | * | Yes | Only remove the listeners that have this context. |
| once | boolean | Yes | Only remove one-time listeners. |
Returns: Phaser.Events.EventEmitter - this.
Source: src/events/EventEmitter.js#L151
Since: 3.0.0
shutdown
<instance> shutdown()
Description:
Removes all listeners from this EventEmitter. This method is called automatically by the Scene Systems when the parent Scene shuts down, ensuring that all event bindings are cleared and no stale references remain.
Source: src/events/EventEmitter.js#L40
Since: 3.0.0