initialize (AsBroadcaster.initialize method)

public static initialize(obj:Object) : Void

Adds event notification and listener management functionality to a given object. This is a static method; it must be called by using the AsBroadcaster class (where someObject is the name of the object to be initialized as an event broadcaster):

AsBroadcaster.initialize(someObject);

Note: A common mistake is to capitalize the second letter of AsBroadcaster. When calling the AsBroadcaster.initialize() method, ensure that the second letter is lowercase. Any misspelling of AsBroadcaster will fail silently.

This method adds the _listeners property along with the following three methods to the object specified by the obj parameter:

Availability: ActionScript 1.0; Flash Player 6

Parameters

obj:Object - An object to serve as a broadcasting object.

Example

The following example creates a generic object, someObject, and turns it into an event broadcaster. The output should be the strings shown in the two trace() statements:

var someObject:Object = new Object(); // Creates broadcast object.

var myListener1:Object = new Object(); // Creates listener object.
var myListener2:Object = new Object(); // Creates listener object.

myListener1.someEvent = function() { // Creates listener method.
    trace("myListener1 received someEvent");
}
myListener2.someEvent = function() { // Creates listener method.
    trace("myListener2 received someEvent");
}

AsBroadcaster.initialize(someObject); // Makes someObject an event broadcaster.
someObject.addListener(myListener1); // Registers myListener1 as listener.
someObject.addListener(myListener2); // Registers myListener2 as listener.
someObject.broadcastMessage("someEvent"); // Broadcasts the "someEvent" message.

The following example shows how to pass extra arguments to a listener method by using the broadcastMessage() method. The output should be the three strings shown in the three trace() statements, which also include the arguments passed in through the broadcastMessage() method.

var someObject:Object = new Object();

var myListener:Object = new Object(); 
myListener.someEvent = function(param1:Number, param2:String) { 
    trace("myListener received someEvent");
    trace("param1: " + param1);
    trace("param2: " + param2);
}

AsBroadcaster.initialize(someObject); 
someObject.addListener(myListener); 
someObject.broadcastMessage("someEvent", 3, "arbitrary string");