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

 

senocular.com ActionScript Library

detectEvents.as

Name: detectEvents/Function.event [MX] - determines what event called the function
Author: senocular: www.senocular.com
Date: 1899-12-31T00:01:51.300
Documentation:
Function EVENT property initiated with DETECTEVENTS
(global).  Intercepts method calls with a getter/setter which
assigns to the set function what call initiated that function
and saves it to an event in that function.  That way,
should a single function be used on more than one event, you can
determine which event called it last (or is calling currently)
by accessing the event property of that function. This can
be accessed in the function using arguments.callee.event

detectEvents([object,] "event1", "event2", ... "eventn");

Arguments:
- object: (optional) an object to detect events for. In using this argument, you can
specify which object in particular you want events to have saved to the event function used
in their definition.  Only the events for this object will then effect the function's event
property default: Object.prototype (all objects)
- events: any number of comma seperated events as strings which are to be the text assigned
to the event property of the assigned function.  When any one of these events are called, the
event property of the function they were assigned is set to the string specified.  Be
concious of the fact that comparing these strings will be case sensitive.

Returns:
- nothing

myFunction.event
- property (string) given to a function if the function was assigned to any event used in
detectEvents and any event set in that manner has been executed.

Example:
 detectEvents("onRollOver", "onPress", "onRelease", "onRollOut", "onReleaseOutside");
generic = function(){
	trace("button event: " + arguments.callee.event);
};
myButton.onRelease = myButton.onPress = myButton.onRollOver = myButton.onRollOut = myButton.onReleaseOutside = generic;
// ^ each event will trace "button event: " and the name of the event, either onRollover, onPress... etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
_global.detectEvents = function(obj){
	var getE, setE, i = -1;
	if (typeof obj == "string") obj = Object.prototype;
	else i = 0;
	while(++i < arguments.length){
		getE = function(){
			var e = arguments.callee.event;
			if (this["$"+e]) {
				this["$"+e].event = e;
				return this["$"+e];
			}
		};
		setE = function(f){
			var e = arguments.callee.event;
			this["$"+e] = f;
			ASSetPropFlags(this,"$"+e,1);
		};
		getE.event = setE.event = arguments[i];
		obj.addProperty(arguments[i], getE, setE);
	}
}