ActionScript 3.0中使用闭包的事件处理程序 [英] ActionScript 3.0 using closures for event handlers

查看:115
本文介绍了ActionScript 3.0中使用闭包的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着这样做:

root.addEventListener("click", 
   function () 
   { 
      navigateToURL(ClickURLRequest,"_self"); 
   });

和它确实增加了事件侦听器。我喜欢用闭包,因为他们在这种情况下工作得很好,

And it does add the event listener. I like using closures because they work well in this situation,

但是,删除事件侦听器需要参照原有功能,因为我用了一个匿名闭包,这是行不通的,我试过了:

however, removing the event listener requires a reference to the original function, and since I used an anonymous closure, it does not work, I tried:

   root.removeEventListener("click", 
       function () 
       { 
          navigateToURL(ClickURLRequest,"_self"); 
       });

以及:

   root.removeEventListener("click", function () {} );

我觉得这是可行的,唯一的办法就是沟匿名闭包并在pre-现有的功能点的事件监听器:

The only way I found it would work was to ditch the anonymous closure and point the event listeners at a pre-existing function:

 function OnClick (e:Event)
 {
     navigateToURL(ClickURLRequest,"_self");
 }

 root.addEventListener("click", OnClick);
 root.removeEventListener("click", OnClick);

有谁知道的方式来使用匿名倒闭事件处理程序,同时仍保留删除它们的能力?

Does anyone know a way to use anonymous closures for event handlers while still retaining the ability to remove them?

推荐答案

下面是删除事件侦听器,我已经用在生产项目的通用方法

Here's a generic way of removing event listeners that i have used on production projects


addEventListener
(
    Event.ACTIVATE, 
    function(event:Event):void
    {
    	(event.target as EventDispatcher).removeEventListener(event.type, arguments.callee)				
    }
)

这篇关于ActionScript 3.0中使用闭包的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆