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

 

senocular.com ActionScript Library

ASEventBroadcaster.as

Name: ASEventBroadcaster [MX] - one event to call many
Author: senocular: www.senocular.com
Date: 1899-12-31T00:16:33.500
Documentation:
Object ASEVENTBROADCASTER: Like that of the
ASBroadcaster but specific to events, where, instead
of running a single event through many objects, this
takes one object and runs many 'events' from one event.

- This can be good for using multiple prototypes which set
common events i.e. if you want to use two prototypes for
one MovieClip and both prototypes set an onEnterFrame for
the movieclip.  This will allow both onEnterFrames to co-
exist. (Note this may not be a flawless solution based on
how those protos are set up)

- Events are stored in an array named event_events
(onMouseDown_event for example) and then that array is
cycled through and run for each function within whenever
the event itself is called.

Usage:

ASEventBroadcaster.initialize(Object,"Event");
- makes the passed event (string) an initialized event within the Object.  Every assignment
of a function to that event in that object will be saved and all will be called when the
event itself is run.
- in initializing an object with any event, it gains the following methods for handling events
- removeEvent;
- soleEvent;
-  clearEvents;
-  broadcastsEvents;

Object.removeEvent("Event" function);
- removes the function from the event.  If the same function is added more than once, only
one is removed.

Object.soleEvent("Event", function [, remove]);
- sets the function of the event to be only the passed function.  If the optional remove
(boolean) is passed (as true), then the ASEventBroadcaster initialization is removed and the
event is reset to normal.

Object.clearEvents("Event" [, remove]);
- Clears all events in the passed event so none are called when the event is run.  If the
optional remove (boolean) is passed (as true), then the ASEventBroadcaster initialization is
removed and the event is reset to normal.

Object.broadcastsEvents("Event");
- returns an array of functions (events) associated with the passed event (a COPY of the actual
list of functions used in the event handling).  If no events are associated with the event,
false is returned. This can be used to tell if an event has been ASEventBroadcaster initialized.


- Adding functions to an event is just a matter of using Object.event = function

this.onEnterFrame = function(){
	//do something
}

Example:
// define functions
a = function(a){ if (a){ trace("A WITH ARG"); }else{ trace("a"); } }
b = function(){ trace("b"); }
c = function(){ trace("c"); }

this.onEnterFrame = a; // onEnterFrame to a initially
ASEventBroadcaster.initialize(this,"onEnterFrame"); // initialize onEnterFrame for this
// ^ the a function is retained

// add more functions to the onEnterFrame call
this.onEnterFrame = b;
this.onEnterFrame = c;
// remove the a event on mouseDown
// the event moved is returned, so this actually calls the function as well (passing 1)
this.onMouseDown = function(){
	this.removeEvent("onEnterFrame",a)(1);
}
// trace the remaining events, clear them,
// then trace them again (false since cleared) on mouseUp
this.onMouseUp = function(){
	trace(this.broadcastsEvents("onEnterFrame"));
	this.clearEvents("onEnterFrame");
	trace(this.broadcastsEvents("onEnterFrame"));
}

// EXAMPLE OUTPUT:
a
b
c
a
b
c
a
b
c
A WITH ARG
b
c
b
c
[type Function],[type Function]
false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
o=_global.ASEventBroadcaster=function(){}
o.removeEvent=function(e,f){ 
	var a=this[e+"_events"];
	var i=a.length;
	while(i--) if(a[i]==f) return a.splice(i,1)[0];
	return false;
}
o.soleEvent=function(e,f,r){
	if (r){
		this.clearEvents(e,r);
		this[e] = f;
	}else this[e+"_events"]=[f];
}
o.clearEvents=function(e,r){
	if (r){
		delete this.removeEvent;
		delete this.soleEvent;
		delete this.clearEvents;
		delete this.broadcastsEvents;
		delete this[e+"_events"];
		delete this[e];
		return true;
	}		
	if (this[e+"_events"]) return this[e+"_events"]=[];
	return false;
}
o.broadcastsEvents=function(e){
	var a=this[e+"_events"];
	if (a.length) return a.slice();
	return false;
}
o.initialize = function(o,e){
	var c = o[e];
	o[e+"_events"] = [];
	var g=function(){
		var a=o[e+"_events"];
		var L=a.length;
		if (L){
			for(var i=0;i<L;i++) a[i].apply(o,null);
			return true;
		}
		return false;
	}
	var s = function(f){
		if (f instanceof Function) return o[e+"_events"].push(f);
		return false;	
	}
	o.addProperty(e,g,s);
	if (c) o[e] = c;
	if (!o.broadcastsEvents){
		o.removeEvent=this.removeEvent;
		o.soleEvent=this.soleEvent;
		o.clearEvents=this.clearEvents;
		o.broadcastsEvents=this.broadcastsEvents;
	}
}
delete o;