如何使用 Javascript 向 Actionscript 发送值 [英] How can I send value to Actionscript using Javascript

查看:14
本文介绍了如何使用 Javascript 向 Actionscript 发送值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向 Actionscript 发送一个 float 变量.

I want to send a float variable to Actionscript.

我使用了window.document.setVariable(),但是它只支持类型String,而忽略了0和point(.)

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

我尝试在 javascript 中使用 parseFloat()value * 1,但它不起作用.

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

推荐答案

你的问题很模糊.不过,这里是:

Your question is pretty vague. Still, here goes:

有 2 个(编辑为 3)方法可以将变量从 html 导入 Flash.两者都使用 ExternalInterface

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

(1): 将变量拉入 ActionScript

(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): 将变量推入 ActionScript

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

编辑(3): 使用 flashVars

EDIT (3): Use flashVars

如果您使用 swfObject,则使用以下行添加 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); 

如果您使用 <object> 标签,那么您可以像这样添加 flashVars:

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>

无论您使用何种嵌入方法,您都可以像这样访问 AS3 中的 flashVars:

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

如果您使用的是 Flex SDK:

If you are using the Flex SDK:

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

如果您没有使用 Flex SDK:

If you are not using the Flex SDK:

var myVariable:Number =Number(LoaderInfo(this.root.loaderInfo).parameters.myVariable);

这篇关于如何使用 Javascript 向 Actionscript 发送值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆