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

 

senocular.com ActionScript Library

booleanMethod.as

Name: booleanMethod() [MX] - translates a boolean variable into method calls
Author: senocular: www.senocular.com
Date: 1899-12-31T00:12:02.100
Documentation:
Object BOOLEANMETHOD: Takes a variable of an object
and interprets its boolean value as a function call as
specified in the definition of booleanMethod. Basically
just setting getter and setter calls for an object property
where the setter calls different functions based on whether
the property is true or false

Arguments:
- prop: (string) The property to be interpreted
- onTrue: (string) The function call to be run in that object if 'prop' is true
- onFalse: (string) The function call to be run in that object if 'prop' is false
- tArgs: (optional) argument array for onTrue
- fArgs: (optional) argument array for onFalse

Returns:
- nothing

Example:
my_mc.goUp = function(speed, message){
	trace(message);
	this._y -= speed;
}
my_mc.goForward = function(speed, message){
	trace(message);
	this._x += speed;
}
my_mc.booleanMethod("mvmnt", "goUp", "goForward", [1,"moving upwards"], [2,"moving forwards"]);
my_mc.onEnterFrame = function(){
	// mvmnt is true is the mouse is down, false if not
	// so if the mouse is down, my_mc goes up, otherwise forward
	this.mvmnt = Key.isDown(1);
}

1
2
3
4
5
6
7
8
Object.prototype.boolMeth = function(prop, onTrue, onFalse, tArgs, fArgs){
	this.addProperty(prop, function(){
		return this[prop];
	},function(bool){
		if (bool) this[onTrue].apply(this, tArgs);
		else this[onFalse].apply(this, fArgs);
	});
}