2012-07-09 63 views
3

如何通過ExternalInterface的方式使EventDispatcher擴展類的「​​addEventListener」對javascript可見,以及如何使它與javascript函數一起使用以便在事件發生時調用js函數閃光物體?ExternalInterface在javascript中使用as3的addEventListener

它可能嗎?或者它可能需要一些技巧才能使其工作?這個想法是讓它變得自然,就像在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什麼,這將是一個電話。

回答

3

你不能像這樣將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(){ 

}); 
+0

它的工作就像一個魅力,我做了注射物進入從Flash中的JavaScript的東西有點非正統的,但非常感謝你。 – khael 2012-07-09 12:15:10

相關問題