How can I send value to Actionscript using Javascript

后端 未结 1 1261
轻奢々
轻奢々 2020-12-22 06:57

I want to send a float variable to Actionscript.

I use window.document.setVariable(), but it only supports type String, and ig

相关标签:
1条回答
  • 2020-12-22 07:36

    Your question is pretty vague. Still, here goes:

    There are 2 (edited to 3) methods to get a variable into Flash from the html. Both use the ExternalInterface class

    (1): Pull the variable into ActionScript

    //JavaScript:
    var myVariable=3.14159; //or whatever you want to set it as
    
    function getMyVariable() {
        return myVariable;
    }
    
    
    //Flash
    var myVariable:Number=ExternalInterface.call("getMyVariable");
    

    (2): Push the variable into ActionScript

    //Flash
    ExternalInterface.addCallback("pushVar", varPushed);
    var myVariable:Number=0;
    function varPushed(x:Number):void {
        myVariable=x;
    }
    
    
    
    //JavaScript
    var myVariable=3.14159; //or whatever you want to set it as
    var fl = document.getElementById('myflashobject');
    fl.pushVar(myVariable);
    

    EDIT (3): Use flashVars

    If you use swfObject, then you add flashVars using the following line:

    var flashvars = {}; 
        flashvars.myVariable=3.14159
    ...
    ...
    swfobject.embedSWF( 
        "FlashVarTest.swf", "flashContent", "100%", "100%", swfVersionStr, 
        xiSwfUrlStr, flashvars, params, attributes); 
    

    If you use the <object> tag then you add flashVars like this:

     <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' height='100%' width='100%'>
            <param name='src' value='FlashVarTest.swf'/>
            <param name='flashVars' value='myVariable=3.14159'/>
            <embed name='mySwf' src='FlashVarTest.swf' height='100%' width='100%' flashVars='myVariable=3.14159'/>
        </object>
    

    Regardless of your embedding method, you access flashVars in AS3 like this:

    If you are using the Flex SDK:

    var myVariable:Number = FlexGlobals.topLevelApplication.parameters.myVariable;
    

    If you are not using the Flex SDK:

    var myVariable:Number =Number(LoaderInfo(this.root.loaderInfo).parameters.myVariable);
    
    0 讨论(0)
提交回复
热议问题