0

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 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Those are not the only ways. You can also use flashvars, which is what the OP is trying to do I think.
@JonatanHedborg, the question is not very clear, but yeah. I'll shortly add the FlashVars part to my answer
@JonatanHedborg, how does it look now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.