Send array from Flash (AS3) to JavaScript

蹲街弑〆低调 提交于 2019-12-21 09:22:11

问题


Is it possible to send an array from Flash (AS3) to JavaScript using an ExternalInterface call?

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.

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.

Thanks, Josh


回答1:


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. Join an array in a string like this in flash:

item1|item2|item3|item4

Pass the string to the JS and split it again using split("|")




回答2:


Yes, it's possible.

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

... arguments — The arguments to pass to the function in the container. You can specify zero or more parameters, separating them with commas. They can be of any ActionScript data type. When the call is to a JavaScript function, the ActionScript types are automatically converted into JavaScript types; when the call is to some other ActiveX container, the parameters are encoded in the request message.

A quick test:

AS code:

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

JS code:

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



回答3:


You could always create a JSON object and pass that to JavaScript.



来源:https://stackoverflow.com/questions/1058589/send-array-from-flash-as3-to-javascript

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