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

 

senocular.com ActionScript Library

setChain.as

Name: setChain [MX] - function calls to follow one another based on a return value comparison
Author: senocular: www.senocular.com
Date: 1899-12-31T00:06:00.000
Documentation:
global function SETCHAIN: set a sequence of functions to
happen one after another. A single function is called every frame
until its return value matches the passed comparison value. If
the value isnt passed, true is assumed. Once the comparison
matches, the next function in the list is called until its
return matches the set comparison value and then continues to
the next for each function passed setting a sequential chain
of function calls.

setChain( array1, array2, array3 [, ... arrayn] );

Arguments:
- any number of comma seperated function setup arrays. Each "function setup array" defines a
function call in the chain.  An array is setup in one of the following manners:
[function, argumentsArray]
[function, argumentsArray, compareValue]
[Object, "method", argumentsArray]
[Object, "method", argumentsArray, compareValue]
[Object, function, argumentsArray]
[Object, function, argumentsArray, compareValue]

- function: a function to be called every frame.  When the return value matches the compareValue
in its setup array, the function call stops and the next setup array (if available) is evaluated.
if Object is used as the first parameter of the array, then the function is run within the scope
of that object.
- argumentsArray: arguments array for the specified function
- compareValue: (optional) the value used to compare to the return value of the current setup
array function when the return value matches this value, the function call is stopped and the
next one begins.  default: true.
- Object: object in which a function or method is called in
- method: (string) the method of the specified object to be run in the function call.

Returns:
- the oEFObject used to handle the function calls of the chain.  An oEFObject is just an object
who recieves an onEnterFrame event (from the attached movieclip broadcaster in _level0).  With
this object you are able to interupt and stop the function calls by nullifying its onEnterFrame
event.  (see below)

Example:
 // generic function
func = function(){
	trace(arguments);
	return Key.isDown(1);
}

// object with method
obj = {};
obj.meth = function(){
	trace(arguments);
	return (!Key.isDown(1)) ? "mouse is up" : "mouse is down";
}

// call function until its return value is true
// then call obj.meth until its return value is "mouse is up"
// then call func in the scope of obj until the return value is true (true since nothing specified)
setChain(	[func, ["a","b"], true], 
	[obj, "meth", [1,2], "mouse is up"],
	[obj, func, ["click once more to stop"]]
);

// example output:
a,b
a,b
a,b
1,2
1,2
1,2
1,2
click once more to stop
click once more to stop

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
/* Setup for Object (oEFObject) with onEnterFrame event */
ASBroadcaster.initialize(_global.onEnterFrame_bc=_level0.createEmptyMovieClip(onEnterFrame_mc,2130690046));
onEnterFrame_bc.onEnterFrame = function(){this.broadcastMessage("onEnterFrame");}
_global.oEFObject = function(){}
oEFObject.prototype.addProperty("onEnterFrame",
	function(){ return this.$onEnterFrame; },
	function(f){
		if (f){ this.$onEnterFrame = f; onEnterFrame_bc.addListener(this);
		}else{ delete this.$onEnterFrame; onEnterFrame_bc.removeListener(this);}
	}
);
oEFObject.prototype.kill = function(parent, varname){
	this.onEnterFrame=0;
	if (arguments.length) delete parent[varname];
	else return this;
}


/* setChain Function */
_global.setChain = function(){
	var e=new oEFObject(), a=arguments, s=0;
	var f,o,p,c,i=function(){
		if (typeof a[s][0]=="function"){
			o=null; f=a[s][0]; p=a[s][1]; c=(a[s].length>2) ? a[s][2] : true;
		}else{
			o=a[s][0]; f=(typeof a[s][1]=="string") ? o[a[s][1]] : a[s][1];
			p=a[s][2]; c=(a[s].length>3) ? a[s][3] : true;
		}};e.onEnterFrame = function(){
		if (f.apply(o,p)==c){
			if (++s>=a.length){
				delete e.kill();
			}else i();
		}};i();
	return e;
}