简体   繁体   English

将数组从Flash(AS3)发送到JavaScript

[英]Send array from Flash (AS3) to JavaScript

Is it possible to send an array from Flash (AS3) to JavaScript using an ExternalInterface call? 是否可以使用ExternalInterface调用将数组从Flash(AS3)发送到JavaScript?

I currently am calling a function multiple times from a 'for each' loop inside Flash but it goes too fast for the JavaScript to keep up. 我目前正在从Flash中的'for each'循环中多次调用一个函数,但它的速度太快,以至于JavaScript无法跟上。

My idea is to create an array of the attributes, pass that to the JavaScript function and then to loop through that in the JavaScript. 我的想法是创建一个属性数组,将其传递给JavaScript函数,然后在JavaScript中循环它。

Thanks, Josh 谢谢,乔希

Yes, it's possible. 是的,这是可能的。

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#call() http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#call()

... arguments — The arguments to pass to the function in the container. ... arguments - 传递给容器中函数的参数。 You can specify zero or more parameters, separating them with commas. 您可以指定零个或多个参数,用逗号分隔它们。 They can be of any ActionScript data type. 它们可以是任何ActionScript数据类型。 When the call is to a JavaScript function, the ActionScript types are automatically converted into JavaScript types; 当调用JavaScript函数时,ActionScript类型会自动转换为JavaScript类型; when the call is to some other ActiveX container, the parameters are encoded in the request message. 当调用其他ActiveX容器时,参数将在请求消息中进行编码。

A quick test: 快速测试:

AS code: AS代码:

if(ExternalInterface.available) {
    ExternalInterface.call("jsTest", [0,1,"two",{a:1,b:2}]);
}

JS code: JS代码:

function jsTest(arg) {
    alert(arg);
}

Further to the suggestion of using JSON, this should be faster for small arrays and wouldn't require the use of eval or an external library to parse. 除了使用JSON的建议之外,对于小型数组来说这应该更快,并且不需要使用eval或外部库来解析。 Join an array in a string like this in flash: 在flash中加入一个像这样的字符串中的数组:

item1|item2|item3|item4 物品1 | ITEM2 |项目3 | ITEM4

Pass the string to the JS and split it again using split("|") 将字符串传递给JS并使用split(“|”)再次拆分它

您始终可以创建一个JSON对象并将其传递给JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM