【问题标题】:How to get the text and rectangle color from flash as3?如何从 flash as3 中获取文本和矩形颜色?
【发布时间】:2012-12-14 10:07:10
【问题描述】:

我想将文本颜色和矩形背景颜色从 Flash 转换为 javascript。 最好的方法是什么?例如,当 Flash 电影加载时,我想将其文本颜色和矩形背景颜色发送到 javascript。然后 javascript 将在 html 文本框中显示此颜色。任何想法,如何做到这一点?
谢谢
灰烬

【问题讨论】:

    标签: javascript actionscript-3 flash flash-cs4


    【解决方案1】:

    您可以使用ExternalInterface

    动作脚本

    在初始化你的 Flash 电影时,你应该添加你想要的可能的回调。这种情况下不需要回调,只需要调用JS即可。只是让你知道怎么做,我会解释怎么做)

    import flash.external.ExternalInterface;
    
    function init(){
        var jsready:Boolean = ExternalInterface.available;
        if(jsready) { //checks if External callbacks can be made
            sendColors();//send the colors when movie is initializing
            try{
                //You add the callback, when JS calls getColors, actionscript will call sendColors() function
                ExternalInterface.addCallback("getColors", sendColors);     
            } catch (error:SecurityError) { 
                trace("A SecurityError occurred: " + error.message + "");
            } catch (error:Error) {
                trace("An Error occurred: " + error.message + "");
            }
        }
    }
    function sendColors(){
        //send your colors to JS
        ExternalInterface.call('receiveColorsFromFlash',color1,color2);
    }
    

    Javascript

    如果你使用了:

    <object id="myflash1">
        <embed id="myflash2">
        </embed>
    </oject>
    

    或:

    <object id="myflash1">
        <object id="myflash2">
        </object>
    </oject>
    

    在代码中嵌入 Flash 的方式,适用于多种浏览器。确保 embed 和 object 标记具有 不同 ID。或者,例如,Firefox 浏览器的第二个对象将不会调用。

    你可以通过添加这个函数来解决这个问题,这个函数总是返回正确的 flash 对象,加载到 DOM 中。这是一个过时的(5 年前的)sn-p,可能不再起作用,请使用 JQuery 或您想要的任何其他解决方案。

    如果您使用另一种嵌入 flashobject 的方式(SWFObject.js 或任何其他方式),您可以只使用 jquery / getElementByid 来定位一个对象。

    function thisMovie() {
            if (navigator.appName.indexOf("Microsoft") != -1) {
                return document.getElementById("myflash1");
            }else if (navigator.vendor.indexOf("Apple") != -1) {
                return document.getElementById("myflash1");
            } else {
                return document.getElementById("myflash2");
            }
    }
    

    Flash会调用的JS函数:

    function receiveColorsFromFlash(color1,color2) {
        //do your thing with the colors
    }
    

    向 flash 请求颜色的 JS 函数

    thisMovie().getColors();
    

    【讨论】:

    • 实际上我知道如何使用外部接口,但感谢您的解释,但我的问题是我将如何将颜色代码从 flash 获取到 javascript....例如我创建一个矩形并给出它是绿色的。现在我想告诉我的 javascript 函数我在这个矩形中使用什么颜色代码。
    • "我如何将颜色代码从 flash 获取到 javascript" ...您实际上还没有阅读我的答案,是吗?如果你想让 flash 告诉你的 javascript 你必须使用 ExternalInterface。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2013-07-26
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多