Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExternalInterface to use as3's addEventListener in javascript

How can you make an EventDispatcher extended class's "addEventListener" visible to javascript by the mean of ExternalInterface, and how to make it work with javascript functions in order to call js functions when events happen in the flash object?

Is it event possible? Or it may require a number of tricks to make it work? The idea is to make it natural, as in just calling the addeventlistener in javascript, and the flash call the javascript callback when an internal event has ocurred?

Ex:

JAVASCRIPT:

flashobj.addEventListener("progress", function (event){alert(event.data);});

Flash:

ExternalInterface.addCallback("addEventListener_", addEventListener);
// as flashObj is a DOM interface after all, I have my reserve in using "addEventListener" as the name for the callback.
... // later in that code:
dispatchEvent(new Event("progress"));
// or
dispatchEvent(new JSEvent("progress"));

The result will be obviously a call to the js function that will alert event.data anything that will be.

like image 403
khael Avatar asked Mar 15 '26 12:03

khael


1 Answers

You cannot pass a javascript function to flash like that.

You can accept listeners in your javascript and fire those from flash though.

ExternalInterface.call( "flash.event", "progress");
ExternalInterface.call( "flash.event", "loaded");
//Etc

Javascript:

var flash = {

    types: {

    },

    event: function( type ) {
        var listeners = this.types[type];

        if( listeners ) {
            for( var i = 0; i < listeners.length; ++i ) {
                listeners[i]();
            }
        }
    },

    addEventListener: function( type, fn ) {
        if( !this.types[type] ) {
            this.types[type] = [];
        }

        this.types[type].push(fn);
    }

};

flash.addEventListener( "progress", function(){

});
like image 196
Esailija Avatar answered Mar 17 '26 03:03

Esailija



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!