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

 

senocular.com ActionScript Library

intercept.as

Name: intercept() - override a method for an object instance
Author: senocular: www.senocular.com
Date: 1899-12-31T00:42:51.600
Documentation:
Object INTERCEPT (and accompanying): temporarily overrides
or intercepts a method or function with a new method or function.
 
[intercept]
- adds override of method to this object instance.  Any call to the overrided method will call
the new method the original was overrided with
 
Arguments:
- method: the method as a string which is to be overrided or intercepted
- newMethod: the new function (as a function reference, not a string) which is to 
replace the original method.
 
Note:
- this is instance specific.  The changes wont apply to all instances of the object. The
example above overrides play only for that movieclip, none others.
- not all methods can be overrided in all objects.  theres no guarantee this will work for
everything everywhere.
- a new function does not have to replace the intercepted one.  null can be used to more or 
less turn the function off until the intercept is removed or changed into something functional.

[deleteIntercepts]
- deletes intercepts added to this object
 
Arguments:
- methods: (optional) any number of string original method names to return back to the object
If no argument is passed all intercepts are deleted.
 
[getIntercept]
- returns the function of the the passed intercept

Arguments:
- method: the (string) name of an intercepted method to get the intercept of
 
Returns:
- the method being used in place of the intercepted method
 
[hasIntercept]
- determines if an instance has a certain intercept

Arguments:
- method: the (string) name of an intercepted method check for
  
Returns:
- 0 if the method does not exist, otherwise the position + 1 of the method in the _intercepts
array object which is created within the object when an intercept is added

Example:
// Simple example that intercepts the play of a movieclip and replaces
// it with a function that uses a goto and play instead.  Since play
// doesnt normally take any arguments, the 1 is ignored until the method
// is intercepted by the new function
onClipEvent(load){
	// define new function used to intercept play()
	newPlay = function(frame){
		this.gotoAndPlay(frame)
	}
}
onClipEvent(mouseDown){
	this.stop(); // stop on mousedown
}
onClipEvent(mouseUp){
	// play on mouse up, if not intercepted plays normally,
	// otherwise to frame 1
	this.play(1); 
}
onClipEvent(keyUp){
	// if the intercept is in place, delete it, otherwise add it
	if (this.hasIntercept("play")) this.deleteIntercepts();
	else this.intercept("play", newPlay); // play now calls newPlay
}

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
Object.prototype.intercept = function(method, newMethod){
	if (!this._intercepts) this._intercepts = [];
	var m = this.hasIntercept(method);
	if (m) m-=1;
	else this._intercepts.unshift([]);
	this._intercepts[m] = [method, (this._intercepts[m][1]) ? this._intercepts[m][1] : this[method], newMethod]; // method name, method ref, new method ref
	this[method] = newMethod;
}
Object.prototype.deleteIntercepts = function(methods){
	if (!this._intercepts.length) return;
	var i = arguments.length;
	if (!i){ // delete all
		i = this._intercepts.length;
		while (i--){
			delete this[this._intercepts[i][0]];
			if (!this[this._intercepts[i][0]]) this[this._intercepts[i][0]] = this._intercepts[i][1];
			delete this._intercepts[i];
		}
		this._intercepts = [];
	}else{ // delete passed
		while (i--){
			var m = this.hasIntercept(arguments[i]);
			if (m){
				delete this[this._intercepts[m-1][0]];
				if (!this[this._intercepts[m-1][0]]) this[this._intercepts[m-1][0]] = this._intercepts[m-1][1];
				delete this._intercepts[m-1];
			}
		}
	}
}
Object.prototype.getIntercept = function(method){
	var i=this.hasIntercept(method);
	if (i) return this._intercepts[i-1][2];
}
Object.prototype.hasIntercept = function(method){
	var i = this._intercepts.length;
	while (i--) if (this._intercepts[i][0] == method) return i+1;
	return 0;
}