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.
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(){
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With