Search code examples
actionscript-3externalinterfacejsfl

using Array argument in jsfl with swfPanel.call


I'm trying to create a swfPanel for Flash CS5.5 using JSFL. I created my interface in flash and try to communicate with a JSFL command. So, in my .fla file, I use an ExternalInterface with callBack to call a specific function in JSFL, and then swfPanel.call() for the return. The problem I encounter is that I can't pass an Array as argument for the call function (after the AS3 function name). Here's the code :

In AS3 :

function init():void{
    ExternalInterface.addCallback("callBackPanel", JsflCallback);
    MMExecute("fl.runScript( fl.configURI + \"AirMobileFramework/AirMobileFrameworkPanel.jsfl\", \"checkSettings\" );");
}

function JsflCallback(... args):void{
    jsTrace("callback");
}

function jsTrace(str:String):void{
    MMExecute("fl.trace(\"" + str + "\");");
}

In JSFL :

function checkSettings(){   
    var fileSettingsUrl = fl.configURI + "AirMobileFramework/settings.fwk";
    var exist = FLfile.exists(fileSettingsUrl);
    var result = new Array("settings", exist);

    if(!exist){
        FLfile.write(fileSettingsUrl, "");
    } else {
        result.push(FLfile.read(fileSettingsUrl));
    }

    callPanelBack(result);
}

function callPanelBack(result){
    fl.trace("result: " + result.length + " > " + typeof result + " >> " + result[0]);
    var panel;
    if(fl.swfPanels.length > 0){ 
        for(x = 0; x < fl.swfPanels.length; x++){
            if(fl.swfPanels[x].name == "AirMobileFramework"){ 
                panel = fl.swfPanels[x];
                panel.call("callBackPanel", result); 
                break; 
            } 
        }
    } else {
        fl.trace("No existing panel");
    }
}

When calling panel.call("callBackPanel", result[0], result[1]); there is no problem, my callback is well called, but when using panel.call("callBackPanel", result); I've an error : La ou les erreurs JavaScript suivantes se sont produites lors de l'exécution de AirMobileFramework : La ou les erreur(s) JavaScript suivantes se sont produites :

Any idea ??


Solution

  • I would guess that it's because JSFL can't parse an array. In the example you give, with result[0], result[1] you have seperate values (probably strigns or numbers), and that should give no problem.

    What I would do is this:

    panel.call("callBackPanel", result.join("@*$");
    

    and in the as3 function simply just arg.split("@*$")

    if your input could have the string "@*$", then you could simply make a call to the SWF for each item in result, and when you're done with the loop there, tell the SWF to gather the things - like this:

    function callPanelBack(result){
        fl.trace("result: " + result.length + " > " + typeof result + " >> " + result[0]);
        var panel;
        if(fl.swfPanels.length > 0){ 
            for(x = 0; x < fl.swfPanels.length; x++){
                if(fl.swfPanels[x].name == "AirMobileFramework"){ 
                    panel = fl.swfPanels[x];
                    panel.call("callBackPanelStart"); 
                    for(var i=0; i < result.length; i++){
                        panel.call("callBackPanelArgument", result[i])
                    }
                    panel.call("callBackPanelEnd"); 
                    break; 
                } 
            }
        } else {
            fl.trace("No existing panel");
        }
    }
    

    and then in AS3:

    function callBackPanelStart():void
    {
        jsflArray = [];
    }
    
    function callBackPanelArgument(argument:*):void
    {
        jsflArray.push(argument);
    }
    
    function callBackPanelEnd():void
    {
        // execute whatever you must on the result
    }