【问题标题】:how to pass arguments from javascript to as3 using external interface如何使用外部接口将参数从 javascript 传递到 as3
【发布时间】:2012-08-09 18:25:00
【问题描述】:

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

TIA。

【问题讨论】:

    标签: javascript actionscript-3 arguments


    【解决方案1】:

    请试试这个:

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

    JS:

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

    【讨论】:

    • 你忘了告诉他在 JS 中做什么
    • 嗨,Edward,javascript 文件怎么样(为了发送参数)。 -- 编辑:Marcelo 已经指出了这一点,抱歉重复评论。
    【解决方案2】:

    根据我的经验,您必须调用 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];)

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2012-10-18
      相关资源
      最近更新 更多