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

 

senocular.com ActionScript Library

FunctionArray.as

Name: Function Array - array of functions to be run in succession
Author: senocular: www.senocular.com
Date: 1899-12-31T00:31:21.600
Documentation:
FUNCTION ARRAY: a simple class which holds functions
in an array which can all be called in succession using
run().
 
[FunctionArray]
- function array class
Arguments:
- scope: (optional) specifies the scope of which the functions are to be run under.  If a
movieclip is passed, the functions will be run as though through that movieclip.  If scope
is not given, the property is set to null, though the run() method allows you to specify a
scope for when the functions are called as well
Returns:
- when used with the new keyword, returns a new function array object.
 
[insert]
- inserts a function or array of functions into the function array
Arguments:
- func: a function or array of functions to be added into the function array
- args: (optional) an array of arguments to be used with the function specified. If
an array of functions is passed, then so should be an array of argument arrays - that is unless
none of the functions passed require arguments, at which case this parameter can be ignored.
- pos: (optional) postion within the array for the function(s) to be inserted. Default is
the end of the array.
 
[positionOf]
- gets the position of a function in the function array
Arguments:
- func: function which is to be found in the function array
Returns:
- returns the position of the passed function in the function array. If more than one function
of the passed function exists, the first function found is returned. If the function does not
exist in the function array, -1 is returned.
 
[run]
- runs all the functions in the array in consecutive order from the first function in the array
to the last.
Arguments:
- scope: (optional) similar to the scope defined when creating the function array, specifies
where the functions are to be run. Defualt is function array object scope.
 
 
 
Tips:
- a function array object has 2 properties, the scope and the actual function array (called
functions). This function array IS an array, so be aware you can perform array operations
on it. The example uses splice in stopMove

Example:
move = function(){
	this._x += 2;
}
fade = function(n){
	this._alpha = this._x/n;
}
stopMove = function(){
	if (this._x > 200) calcs.functions.splice( calcs.positionOf(move), 1 );
}
 
calcs = new FunctionArray(clip); // make new function array to be run in clip (a movieclip)
calcs.insert(fade, [4]); // add in fade with argument of 4
calcs.insert([move,stopMove],null,0); // add move and stopMove into the beginning of the array (position 0)
 
clip.onEnterFrame = function(){
	calcs.run(); // run calcs every frame of clip
}
// new functions can easily be added to the clips on enter frame just by adding
// them into the calcs array

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
FunctionArray = function(scope){
	this.functions = []; // array of functions/arguments to be called
	if (arguments.length){ this.scope = scope; // add scope if given
	}else{ this.scope = null;} // else null
}
FunctionArray.prototype.insert = function(func, args, pos){
	var i, l, temp = []; // temp vars
	if (arguments.length == 3){ temp = this.functions.slice(pos); // save array after pos in temp
	}else{ var pos = this.functions.length;} // set pos to end of the array
	var type = typeof(func); // get type of argument func is
	if (type == "function"){ // if just one function
		if (arguments.length == 1) var args = null; // if no arguments, set to null
		this.functions[pos] = [func, args]; // add function and arguments
	}else if (type == "object"){ // if an array of functions, add each one
		var i, l=func.length; // temp vars
		for (i=0;i<l;i++) this.functions[pos+i] = [func[i], args[i]]; // add each function and argument array
	}
	if (l = temp.length) for (i=0;i<l;i++) this.functions[this.functions.length] = temp[i]; // append temp on the end of the array
}
FunctionArray.prototype.positionOf = function(func){
	var i, l=this.functions.length; // temp vars
	for (i=0;i<l;i++) if (func == this.functions[i][0]) return i; // search for position in array
	return -1; // if not found return -1
}
FunctionArray.prototype.run = function(scope){
	if (!arguments.length) var scope = this.scope; // use this scope if not passed
	var i,l=this.functions.length; // temp vars
	for (i=0;i<l;i++) this.functions[i][0].apply(scope, this.functions[i][1]); // call each function with arguments	
}