Event Model

As shown in Figure 1-2, all Starling objects are subclasses of the EventDispatcher class. Just like with the native EventDispatcher API, all Starling objects expose APIs to add or remove listeners to them:

addEventListener

Registers a listener to a specific event.

hasEventListener

Tests if there is a listener for a specific event.

removeEventListener

Removes the event listener.

removeEventListeners

Removes all the listeners registered to a specific event or all of them.

Note the addition of a very useful API: removeEventListeners. At any time, when you need to remove all listeners registered to a specific event, use removeEventListeners by passing the event type:

button.removeEventListeners(Event.TRIGGERED);

When you need to remove all the listeners, no matter the event, in a scenario where you are about get rid of an object or deactivate it, just call the same removeEventListeners API with no event type as a parameter:

button.removeEventListeners ();

Note that we just used recently the removeChild API that takes an argument for disposing listeners, which calls internally the same API internally, but on each child.

Event Propagation

As we have seen since the beginning of this Starling tutorial, Starling recreates the concept of display list on top of Stage3D. The great news is that you will be able to also reuse the power of event propagation with Starling. Event propagation can be really useful in some scenarios to limit the number of listeners that you have to register and unregister ...

Get Introducing Starling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.