简体   繁体   English

如何从Flash As3获取文本和矩形颜色?

[英]How to get the text and rectangle color from flash as3?

I want to get the text color and rectangle background color from flash to javascript. 我想从Flash到javascript获取文本颜色和矩形背景颜色。 what is the best way for this? 最好的方法是什么? for example, when flash movie will load i want to send its text color and rectangle background color to javascript. 例如,当要加载Flash电影时,我想将其文本颜色和矩形背景颜色发送给javascript。 then javascript will show this color in html textbox. 然后javascript将在html文本框中显示此颜色。 any idea, how to do this? 任何想法,该怎么做?
thanks 谢谢
Ashish 阿希什

You can use the ExternalInterface class 您可以使用ExternalInterface

ActionScript 动作脚本

On initializing your flash movie you should add possible callbacks you want. 初始化Flash电影时,您应该添加所需的回调。 In this case you don't need a callback, you just need to call JS. 在这种情况下,您不需要回调,只需要调用JS。 Just so you know how to do it anyway, i'll explain how ) 以便您无论如何都知道如何操作,我将向您解释)

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 使用Javascript

If you used the: 如果您使用了:

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

or the: 或者:

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

Way of embedding your flash in the code, for multiple browsers. 将Flash嵌入代码中的方式,适用于多个浏览器。 Make sure the embed and object tag have different ID's. 确保embed和object标签具有不同的 ID。 Or the calls will not be made for the 2nd object for firefox browsers for example. 否则,例如将不会为Firefox浏览器的第二个对象进行调用。

You can solve this by adding this function which always returns the correct flash object, loaded into DOM. 您可以通过添加此函数来解决此问题,该函数始终返回加载到DOM中的正确Flash对象。 This is an outdated (5y old) snippet, and might not work anymore, use JQuery or any other solution you'd like for this. 这是一个过时的(5岁以下)代码段,可能不再起作用,请使用JQuery或您想要的任何其他解决方案。

If you use another way of embedding flashobject (SWFObject.js or any other) You could just use jquery / getElementByid to target the one object. 如果您使用另一种嵌入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");
        }
}

The JS function which Flash will call: Flash将调用的JS函数:

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

The JS function that asks flash for colors 要求闪光灯提供颜色的JS函数

thisMovie().getColors();

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

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