5) Basics

Before exploring the class API, remember that EventCollector instances must be accessed from any part of your object, but always hidden from the public API. (See Chapter 8 for exceptions.)

Most of the time, we will only use two methods for managing events: addEvent() and removeEvent().

All parameters of the addEvent() method are the same as the parameters of the addEventListener() method, defined by the IEventDispatcher interface, except the first one, which is the listener object itself.

addEvent(obj:IEventDispatcher, event:String, func:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Note that the event and the func parameters of the addEvent() method represent respectively the event and listener parameters of the addEventListener() method.

The common use of the EventCollector class is shown below:

// Creates a new  EventCollector instance
var evtColl:EventCollector = new EventCollector();
// Creates a new  Loader instance
var loader:Loader = new Loader();
// Adds a new event handler to the event collection
evtColl.addEvent(loader.contentLoaderInfo, Event.COMPLETE, loaderComplete);
function  loaderComplete(e:Event):void {
    trace("Data loaded");

The following code does the same without using an EventCollector instance:

// Creates a new  Loader instance
var loader:Loader = new Loader();
// Adds a new event handler to the  loader object
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

To remove the registered event handler, you call the removeEvent() method with only three parameters: the listener object, the event type and the associated function. The code below shows how to remove the event handler we added above:

evtColl.removeEvent(loader.contentLoaderInfo, Event.COMPLETE, loaderComplete);

This is the minimal API you need to know to work with the EventCollector class.
The event collection process provides many more methods to implement mechanisms based on the IEventDispatcher interface (e.g. hasRegisteredEvent()), or custom convenient methods (e.g. addEventCollection()). For more information about methods of the EventCollector class, go to the SPAS 3.0 documentation at: http://www.flashapi.org/spas-doc/