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

 

senocular.com ActionScript Library

delayBy.as

Name: Function.prototype.delayBy() - (MX) function stops functioning for set amount of calls
Author: senocular: www.senocular.com
Date: 1899-12-31T00:59:47.600
Documentation:
Function DELAYBY (MX): sets a function to stop working
for a set amount of calls.  After the function has been called the
number of times its been delayed, it regains functionality.  delayBy()
also adds a _delay property to the function which represents the number
of calls left before the function begins to function properly again.
This property is reset each time delayBy() is called.
 
Arguments:
- delay: an integer specifying the number of times a function is to be delayed
before further calls to that function work properly.
 
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.


Example:
function hit(n){
	trace("hit #"+n);
}
hit = hit.delayBy(5);
for (i=0;i<10;i++) hit(i);
// output:
"hit #5"
"hit #6"
"hit #7"
"hit #8"
"hit #9"

trace(hit._delay); // 0
 
clip.onEnterFrame = function(){
	this._x++;
}
clip.onEnterFrame = clip.onEnterFrame.delayBy(30); // clip only moves after 30 frames

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.delayBy = function(delay){
	var me = this;
	if (this._delay == undefined){
		this = function(){
			if(!arguments.callee._delay) return me.apply(this, arguments);
			else arguments.callee._delay--;
		}
	}
	this._delay = delay;
	return this;
}