Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept multiple arguments in an AS3 method

How do I accept multiple arguments in a custom method? Like:

Proxy(101, 2.02, "303");

function Proxy(args:Arguments){
    Task(args);
}

function Task(var1:int, var2:Number, var3:String){ 
    // work with vars
}
like image 336
Robin Rodricks Avatar asked Dec 09 '22 18:12

Robin Rodricks


2 Answers

You wouldn't be able to just pass the args array through like you have in your question. You'd have to pass each element of the args array seperately.

function Proxy(... args)
{
   // Simple with no error checking.
   Task(args[0], args[1], args[2]);
}

Udate

After reading one of the comments, it looks like you can get away with:

function Proxy(... args)
{
    // Simple with no error checking.
    Task.apply(null, args);

    // Call could also be Task.apply(this, args);
}

Just be careful. Performance of apply() is significantly slower than calling the function with the traditional method.

like image 107
Justin Niessner Avatar answered Dec 20 '22 16:12

Justin Niessner


You can also use apply(thisArg:*, argArray:*):* method from the Function object.

example:

package{

    public class Test{
          public function Test(){
              var a:Array=[1,"a"];
              callMe.apply(this,a);
          }       
          public function callMe(a:Number,b:String):void{
                trace(a,b);
          }
    }
}
like image 42
OXMO456 Avatar answered Dec 20 '22 17:12

OXMO456