This is the personal weblog of Greg Burch.

Some Interesting Code

So I made some code for fun after seeing a post on ultrashock (I am Recalled on there by the way). This code uses some new features of MX that some may or may not know about. These are the apply and __resolve methods. 

Function.apply(thisObject, argumentsObject)

I have known about for a while, and have used in many cases, and most of you probably know about it to, but here is what the definition from the Refernce panel in Flash is:

Description:

Method; specifies the value of this to be used within any function that ActionScript calls. This method also specifies the parameters to be passed to any called function. Because apply is a method of the Function object, it is also a method of every function object in ActionScript.

The parameters are specified as an Array object. This is often useful when the number of parameters to be passed is not known until the script actually executes.

So that can be very useful. I use it in the Glyph.as and TweenEngine.as file to make sure my dynamic facades are parameter agnostic. (There has also been tons of talk about facades lately, the Glyph system has an interesting twist on it)

__resolve

I first saw this in Flash Remoting and wondered how they could catch method calls, when no method existed. I found this method. This is used in Glyph.as to "forward" on method calls and parameters (after adding one) to the correct class. So if __resolve exists it is called when an unknown class or variable is called (variable only on the right side of the =). It is passed one parameter which is the name of the unknown. It is then your job to return a function to  be called in its place, from __resolve.

Example:

Glyph.prototype.__resolve = function(methodName){
	return function(){
		arguments.unshift(this.ref_mc);
		return this.ref_class[methodName].apply(this.ref_class,arguments);
	}
}
So in this example I get the methodName in my function, and return a function that takes the arguments, adds a parameter and then finally forwards on the call to the internal classes method (phew) If you get a chance play with the TweenEngine.as and see how you can embed engines within each other to create "macros" I thought it was a pretty cool piece of code. Example:
function foo(str){
 trace(str);
}

engine1 = new TweenEngine(); cmd1 = {meth: "foo", obj: this, params:["\t\t\tIt Works"]} engine1.addCommand("foo", this, ["\t\tEngine 1 Started"]); engine1.addCommands([cmd1,cmd1,cmd1]); engine1.addCommand("foo", this, ["\t\tEngine 1 Completed"]); engine2 = new TweenEngine(); engine2.addCommand("foo", this, ["\tEngine 2 Started"]); engine2.addCommand("runNextCommand",engine1); engine2.addCommand("runNextCommand",engine1); engine2.addCommand("runNextCommand",engine1); engine2.addCommand("runNextCommand",engine1); engine2.addCommand("foo", this, ["\tEngine 2 Completed"]); engine3 = new TweenEngine(); engine3.addCommand("foo", this, ["Engine 3 Started"]); engine3.addCommand("runNextCommand",engine2); engine3.addCommand("runNextCommand",engine2); engine3.addCommand("foo", this, ["Engine 3 Completed"]); engine3.startEngine();

OUTPUT:
Engine 3 Started
 Engine 2 Started
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
 Engine 2 Completed
 Engine 2 Started
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
  Engine 1 Started
   It Works
   It Works
   It Works
  Engine 1 Completed
 Engine 2 Completed
Engine 3 Completed


Interesting.



© Copyright 2003 Greg Burch. Click here to send an email to the editor of this weblog.
Last update: 2/13/2003; 2:38:08 PM.