如何使用外部接口将参数从 JavaScript 传递到 AS3

how to pass arguments from javascript to as3 using external interface

本文关键字:JavaScript AS3 参数 何使用 外部 接口      更新时间:2023-09-26

我找到了如何从javascript调用actionscript,但我也需要传递一些参数(动态),我该怎么做?

蒂亚。

请尝试以下操作:

ExternalInterface.addCallback("sendMsg", generateMsg);
function generateMsg(str):void {
    trace(str);
}

.JS:

msg = "";
function setMsg(myMsg) {
    msg = myMsg;
    SendDataToFlashMovie(myMsg);
}

根据我的经验,您必须在 flash 对象上调用该函数。

我使用以下 javascript 函数来获取 flash 对象

function GetSWF(id) {
    if (window.document[id] != null)
        if (window.document[id].length == null)
            return window.document[id];
    else
        return window.document[id][1];
    else
    if  (typeof(document[id]) == 'undefined')
        return $('#'+id)[0];
    else
    if (document[id].length == null)
        return document[id];
    else
        return document[id][1];
}

然后按如下方式调用函数

var flash = GetSWF('idOfSWF');
if (typeof flash.sendToActionScript === 'function'){
    flash.sendToActionScript(yourObject,orParameter);
}

AS3 如下所示

if (ExternalInterface.available){
    ExternalInterface.addCallback("sendToActionScript",receivedFromJavascript);
}
function receivedFromJavascript(myObject:Object,myParameter:String):void{
    // Do something
}

希望这有帮助。

编辑:

刚刚注意到我在GetSWF函数中有少量的jQuery用法。我会看看并尝试删除它。(它的行return $('#'+id)[0];