Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

listenTo.as

Name: listenTo() - make listener listen to multiple broadcasters
Author: senocular: www.senocular.com
Date: 1899-12-31T00:44:16.500
Documentation:
Object LISTENTO (for ASBroadcaster uses): When used on a 
listener, it lets you specify more than one broadcaster for
that object to listen to (it adds this object as a listener
to the passed objects)
 
Arguments:
- objs: any number of objects for which the current object is to be a listener
of.  If any of the passed objects were not initialized with ASB initialize (or internally
dont have an addlisteners handler) then the object will be initialized before this as a
listener is added.

Example:
Admiral = {};
General = {};

Troop = {};
Troop.yelling = function(){ trace("Sir, yes SIR!"); }
Troop.listenTo(Admiral, General);

Admiral.broadcastMessage("yelling");
General.broadcastMessage("yelling");

// traces:
Sir, yes SIR!
Sir, yes SIR!

1
2
3
4
5
6
7
8
9
10
// since broadcasting objects arent instances of the broadcaster
// object and are just general objects themselves, Object.prototype
// is used for initiated ASB objects (and listeners)
Object.prototype.listenTo = function(objs){
	var i = 0; l = arguments.length;
	for (i=0; i<l; i++){
		if (!arguments[i].addListener) ASBroadcaster.initialize(arguments[i]);
		arguments[i].addListener(this);
	}
}