EventDispatcher

Type class

Universal Event Dispatcher.

Click here for more info

Sources

This information about EventDispatcher is provided for reference only and is not available through exported packages and thus should not be imported or used on its own.

Inheritance

EventDispatcher does not extend any other symbol.

EventDispatcher is extended by SpriteEventDispatcher.

Settings

EventDispatcher does not have any settings.

Private settings

EventDispatcher does not have any private settings.

Properties

EventDispatcher does not have any properties.

Methods

constructor()

#

Returns EventDispatcher

Constructor

disable()

#

Returns void

Disable dispatching of events until re-enabled by enable().

disableType(

type: Key,
amount: number

)

#

Returns void

Stops dispatching events of type, until enableType() is called or, if amount is set, for that many dispatches.

dispatch(

type: Key,
event: T[""]

)

#

Returns void

Calls the listeners of type right away; debounced listeners are called after their delay. event must include type and target.

dispose()

#

Returns void

Disposes the dispatcher and all its listeners.

enable()

#

Returns void

Enable dispatching of events if they were previously disabled by disable().

enableType(

type: Key

)

#

Returns void

Enable dispatching particular event, if it was disabled before by disableType().

has(

type: Key,
callback?: undefined | ( this: C, event: T[""]) => void,
context?: C

)

#

Returns boolean

Checks if there's already a listener with specific parameters. Listeners added with once() are not counted.

hasListeners()

#

Returns boolean

Checks if the dispatcher has any listeners.

hasListenersByType(

type: Key

)

#

Returns boolean

Checks if the dispatcher has listeners for type, counting the ones added with onAll().

isDisposed()

#

Returns boolean

Returns true if the dispatcher has been disposed.

isEnabled(

type: Key

)

#

Returns boolean

Checks if an event of type would reach a listener: events are enabled, the type is not disabled, and it has listeners.

off(

type: Key,
callback?: undefined | ( this: C, event: T[""]) => void,
context?: C

)

#

Returns void

Removes listeners added with on() or onDebounced() that match type, callback and context. Without callback, removes all of them for type and context. Listeners added with once() are not removed.

on(

type: Key,
callback: ( this: C | undefined, event: T[""]) => void,
context?: C,
shouldClone: boolean

)

#

Returns IDisposer

Creates an event listener to be invoked on a specific event type.

button.events.on("click", (ev) => {
  console.log("Button clicked");
}, this);
button.events.on("click", function(ev) {
  console.log("Button clicked");
}, this);

The above logs every click on the button. To remove the listener, call dispose() on the returned object, or use off().

onAll(

callback: ( this: C, event: T[""]) => void,
context?: C,
shouldClone: boolean

)

#

Returns IDisposer

Creates an event listener to be invoked on any event.

onDebounced(

type: Key,
callback: ( this: C | undefined, event: T[""]) => void,
debounceDelay: number,
context?: C,
shouldClone: boolean

)

#

Returns IDisposer

Creates a debounced event listener to be invoked on a specific event type.

button.events.onDebounced("click", (ev) => {
  console.log("Button clicked");
}, 500, this);
button.events.onDebounced("click", function(ev) {
  console.log("Button clicked");
}, 500, this);

The above logs once, 500 ms after the last of a series of clicks. Each new event restarts the delay, and callback gets the last one.

Click here for more info
@since 5.14.0

once(

type: Key,
callback: ( this: C | undefined, event: T[""]) => void,
context?: C,
shouldClone: boolean

)

#

Returns IDisposer

Creates an event listener that is invoked only the first time the event happens, and is then disposed.

button.events.once("click", (ev) => {
  console.log("Button clicked");
}, this);
button.events.once("click", function(ev) {
  console.log("Button clicked");
}, this);

The above logs the first click on the button only. off() does not remove such a listener: call dispose() on the returned object instead.

removeType(

type: Key

)

#

Returns void

Removes all listeners of a particular event type.

Events

EventDispatcher does not have any events.