سؤال

I want to get the text color and rectangle background color from flash to 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. then javascript will show this color in html textbox. any idea, how to do this?
thanks
Ashish

هل كانت مفيدة؟

المحلول

You can use the ExternalInterface class

ActionScript

On initializing your flash movie you should add possible callbacks you want. In this case you don't need a callback, you just need to call 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

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. Make sure the embed and object tag have different ID's. Or the calls will not be made for the 2nd object for firefox browsers for example.

You can solve this by adding this function which always returns the correct flash object, loaded into DOM. This is an outdated (5y old) snippet, and might not work anymore, use JQuery or any other solution you'd like for this.

If you use another way of embedding flashobject (SWFObject.js or any other) You could just use jquery / getElementByid to target the one object.

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:

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

The JS function that asks flash for colors

thisMovie().getColors();

نصائح أخرى

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top