Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 arguments

Why do you think the code below does not work? What would you change/add to make it work?

Any help is appreciated..

function TraceIt(message:String, num:int)
{
    trace(message, num);
}

function aa(f:Function, ...args):void
{
    bb(f, args);
}

aa(TraceIt, "test", 1);

var func:Function = null;
var argum:Array = null;

function bb(f:Function, ...args):void
{
    func = f;
    argum = args;
    exec();
}

function exec()
{
    func.apply(null, argum);
}

I get an ArgumentError (Error #1063):

Argument count mismatch on test_fla::MainTimeline/TraceIt(). Expected 2, got 1.

..so, the passed parameter (argum) fails to provide all passed arguments..

..Please keep the function structure (traffic) intact.. I need a solution using the same functions in the same order.. I have to pass the args to a variable and use them in the exec() method above..

regards

like image 625
Onur Yıldırım Avatar asked Apr 23 '26 22:04

Onur Yıldırım


1 Answers

Ok, here is the solution.. after breaking my head : )

    function TraceIt(message:String, num:int)
    {
        trace(message, num);
    }

    function aa(f:Function=null, ...args):void
    {
        var newArgs:Array = args as Array;
        newArgs.unshift(f);
        bb.apply(null, newArgs);
    }

    aa(TraceIt, "test", 1);

    var func:Function = null;
    var argum:*;

    function bb(f:Function=null, ...args):void
    {
        func = f;
        argum = args as Array;
        exec();
    }

    function exec():void
    {
        if (func == null) { return; }
        func.apply(this, argum);
    }

This way, you can pass arguments as variables to a different function and execute them..

Thanks to everyone taking the time to help...

like image 124
Onur Yıldırım Avatar answered Apr 25 '26 19:04

Onur Yıldırım