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

 

senocular.com ActionScript Library

endAfter.as

Name: Function.prototype.endAfter() - (MX) function only works for set amount of calls
Author: senocular: www.senocular.com
Date: 1899-12-31T00:10:44.500
Documentation:
Function ENDAFTER (MX): sets a function to only run a set
amount of times. Every call of that function following the number of
times specified will yield no result. endAfter also adds a _called
property to the function which counts how many times its been called
after endAfter is used.  It also adds an _anedAt property which
signifies the point at which it ended.  These properties are reset
each time endAfter() is called.
 
Arguments:
- endAt: an integer specifying the number of times a function is to run before further
calls to that function are ignored.
 
Returns:
- returns a copy of itself which will end after set number of calls. If not set to itself,
the original function will continue to operate normally.
 
Update:
- allowed for endAfter to be called more than once. whether or not your function has 
ended based on a previous call of endAfter() a new call of endAfter will reset the 
conditions to follow with the newest call.
 
Warning:
- endAfter may have unusual effects when combined with other function definition altering
prototypes like addIn().

Example:
function hit(){
	trace("hit");
}
hit = hit.endAfter(5);
for (i=0;i<10;i++) hit(); // traces "hit" 5 times though its called 10
trace(hit._called); // 10
 
clip.onEnterFrame = function(){
	this._x++;
}
clip.onEnterFrame = clip.onEnterFrame.endAfter(30); // clip only moves 30 places

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.endAfter = function(endAt){
	var me = this;
	if (this._called == undefined){
		this = function(){
			if(arguments.callee._called++ < arguments.callee._endAt) return me.apply(this, arguments);
		}
	}
	this._called = 0;
	this._endAt = endAt;
	return this;
}