How can I send value to Actionscript using Javascript

只谈情不闲聊 提交于 2019-12-29 09:37:07

问题


I want to send a float variable to Actionscript.

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

I try to use parseFloat() and value * 1 in javascript,but it doesn't work.


回答1:


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);


来源:https://stackoverflow.com/questions/8200861/how-can-i-send-value-to-actionscript-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!