I want to send a float
variable to Actionscript.
I use window.document.setVariable()
, but it only supports type String
, and ig
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);