///////////////////////////////// Command Class 1.09 /////////////////////////////////// // Tatsuo Kato Nov. 2002 //////////////////////////////////////////////////////////////////////////////////////// /* Command new Command - Constructor for the Command Object Availability Flash Player 6. Usage 1. new Command(anonymousFunction); 2. new Command(myObj, methodName, arg1, arg2, ...); 3. new Command(functionExecutionString); Parameters 1. An anonymous function, a.k.a. a function object. 2. A set of an object that owns the method, the method name and parameters to be passed to the method. 3. A string of a function call Returns A Command object Description Constructor; creates a new Command object which is storable and call-ready. Example //user function example mc1.move = function (x, y) { this._x += Number(x); this._y += Number(y); } //Note: Number() conversion is only necessary for #3 in the following example. //Create a command object using one of the following three ways. //#1 myCommand = new Command(function(){_root.mc1.move(100, 50);}); //Note: //_global will be the caller object of the passed function //if the command object is created this way. //#2 myCommand = new Command(mc1, "move", 100, 50); //#3 myCommand = new Command("_root.mc1.move(100,50)"); //Note: //The path to the method via the object should be absolute. //And there should not be any white space/CR/tab in the command string. //Then it can be stored somewhere and executed at any time you want. myCommand.execute(); //If the subscribed method has been set to return something, //myCommand.execute() returns it. execute Availability Flash Player 6. Usage myCommand.execute(); Parameters None. Returns What the 'method' property of the Command object returns. If the method returns nothing, myCommand.execute() returns nothing. Description Method; executes the method, which is the 'method' property of the Command object in the scope of the 'obj' property of the Command object. If the Command object has no 'obj' property, the command gets executed in the scope of '_global'. callback Availability Flash Player 6. Usage Command.callback(); Parameters None. Returns What the 'method' property of the Command object returns. If the method returns nothing, myCommand.execute() returns nothing. Description Method; is used to specify when the onComplete handler of the Command object should happen. Example //To get a callback from the Command object, add Command.callback() at the end //of the definition of the method to be passed to the new Command() constructor. mc1.move = function (x, y) { this._x += x; this._y += y; Command.callback();//This executes myCommand.onComplete handler. //With Sequence class, this is needed in order to create a command chain. } myCommand = new Command(mc1, "move", 100, 50); myCommand.onComplete = function() { trace("Complete"); } */ _global.Command = function() { if (arguments[0] == undefined) return; var a = arguments[0]; if (arguments.length > 1) { this.obj = a; this.method = a[arguments[1]]; this.args = arguments.slice(2); } else if (a instanceof Function) { this.method = a; } else if (typeof a == "string") { var idxOfParensL = a.indexOf("("); var pathToMethod = a.slice(0, idxOfParensL); this.obj = eval(pathToMethod.slice(0, pathToMethod.lastIndexOf("."))); this.method = eval(pathToMethod); this.args = a.slice(idxOfParensL + 1, a.lastIndexOf(")")).split(","); } }; Command.prototype.execute = function() { var cmd = this; Command.callback = function(params) { Command.data = arguments; cmd.callbackCommand.execute(); cmd.onComplete(); /* 'callbackCommand' is a Command object for internal use such as in Sequence class, while 'onComplete' is an event handler for user. */ }; /* What is assigned to Command.callback is for the 'method' to execute the callbackCommand, to call the onComplete handler, and to store data in 'Command.data' which may be used by some other object. The assignments to 'callbackCommand', 'onComplete' and the parameters are all optional. In the definition of the 'method', if it's momentary, just do: Command.callback(); at the end the method definition. Optionally, if parameters are passed, they are stored in an array 'Command.data' temporarily so the method of the next Command object, for example, can use them. And if the 'method' is asynchronous, first do: this.callback = Command.callback; //doesn't have to be 'this' actually. Can be anywhere you want to store it And then where the command is about to finish, just call it. this.callback(); */ return this.method.apply(this.obj, this.args); }; ////////////////////////////////////////////////////////////////////////////////////////