如何通过 ExternalInterface 使 EventDispatcher 扩展类的“addEventListener”对 javascript 可见,以及如何使其与 javascript 函数一起工作以便在 flash 对象中发生事件时调用 js 函数?
事件可能吗?或者它可能需要一些技巧才能使其工作?这个想法是让它变得自然,就像在 javascript 中调用 addeventlistener,当内部事件发生时 flash 调用 javascript 回调?
例如:
JavaScript:
flashobj.addEventListener("progress", function (event){alert(event.data);});
闪光灯:
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"));
结果显然是对 js 函数的调用,该函数将向 event.data 发出任何警报。
最佳答案
您不能像那样将 javascript 函数传递给 flash。
您可以在您的 javascript 中接受监听器并从 flash 中触发它们。
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(){
});
关于javascript - ExternalInterface 在 javascript 中使用 as3 的 addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11390286/