javascript - ExternalInterface 在 javascript 中使用 as3 的 addEventListener

标签 javascript actionscript-3 event-handling flashdevelop

如何通过 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/

相关文章:

javascript - 耐克斯种子 : insert out of order from promise

apache-flex - Adobe Flex 应用程序页面文件使用量激增!

flash - 阶段调整大小并获得正确的变量

Spring 事件和范围请求

javascript - 迷失在 javascript 范围内

c# - 在 C#/Winforms 中使 Web 服务方法异步

javascript - JavaScript 中的完全限定名称

javascript - 从 React Native 编辑 float 标签和按钮

javascript - 如何在头部设置标题样式 (html)

flash - 求弧度反射角