2011-03-09 121 views

回答

3

只有當你一直仔細追蹤你的聽衆。事件監聽器沒有「removeAll」類型的功能,因此它有助於在易於管理的位置收集監聽器。

+0

這個。事實上,不僅「事件偵聽器」沒有「全部刪除」功能,除了「你對ENTER_FRAME做出響應嗎?」之外,沒有辦法做任何檢查,所以你甚至不能進行徹底的遞歸檢查並刪除那些你找到的,因爲你無法讀取事件的目標。你最好的選擇是注意你如何添加它們。 – 2011-03-10 20:03:02

2

一個選項是創建一個類,用於處理將事件偵聽器添加到IEventDispatcher對象中,註冊這些對象並同時刪除所有事件偵聽器。以下是我迅速做了演示這樣的例子:

EventListeners.as:

package 
{ 
    import flash.events.Event; 
    import flash.events.IEventDispatcher; 
    import flash.utils.Dictionary; 

    public class EventListeners 
    { 
     private var _objectDictionaries:Vector.<Dictionary>; 

     public function EventListeners() 
     { 
      init(); 

     }// end function 

     public function addEventListener(object:IEventDispatcher, 
             type:String, 
             listener:Function, 
             useCapture:Boolean = false, 
             priority:int = 0, 
             useWeakReference:Boolean = false) 
     { 
      object.addEventListener(type, listener, useCapture, priority, useWeakReference); 

      var objectDictionary:Dictionary = new Dictionary(); 
      objectDictionary["object"] = object; 
      objectDictionary["type"] = type; 
      objectDictionary["listener"] = listener; 
      objectDictionary["useCapture"] = useCapture; 
      _objectDictionaries.push(objectDictionary); 

     }// end function 

     public function removeAll():void 
     { 
      for each(var objectDictionary:Dictionary in _objectDictionaries) 
      { 
       var object:IEventDispatcher = objectDictionary["object"] as IEventDispatcher; 
       var type:String = objectDictionary["type"] as String; 
       var listener:Function = objectDictionary["listener"] as Function; 
       var useCapture:Boolean = objectDictionary["useCapture"] as Boolean; 

       object.removeEventListener(type, listener, useCapture); 

      }// end for 

      init(); 

     }// end function 

     private function init():void 
     { 
      _objectDictionaries = new Vector.<Dictionary>(); 

     }// end function 

    }// end class 

}// end package 

Main.as:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 
     private var _eventListeners:EventListeners; 
     private var _sprite1:Sprite; 
     private var _sprite2:Sprite; 

     public function Main() 
     { 
      init(); 

     }// end function 

     public function init():void 
     { 
      _eventListeners = new EventListeners(); 
      _sprite1 = new Sprite(); 
      _sprite2 = new Sprite(); 

      _eventListeners.addEventListener(_sprite1, Event.ENTER_FRAME, enterFrameHandler); 
      _eventListeners.addEventListener(_sprite2, Event.ADDED_TO_STAGE, addedToStageHandler); 

      trace("_sprite1.hasEventListener = " + _sprite1.hasEventListener(Event.ENTER_FRAME)); 
      trace("_sprite2.hasEventListener = " + _sprite2.hasEventListener(Event.ADDED_TO_STAGE)); 

      _eventListeners.removeAll(); 

      trace("\n_eventListeners.removeAll() invoked\n"); 

      trace("_sprite1.hasEventListener = " + _sprite1.hasEventListener(Event.ENTER_FRAME)); 
      trace("_sprite2.hasEventListener = " + _sprite2.hasEventListener(Event.ADDED_TO_STAGE)); 

     }// end function 

     private function enterFrameHandler(e:Event):void {}; 
     private function addedToStageHandler(e:Event):void {}; 

    }// end class 

}// end package 

運行這個例子的輸出如下:

_sprite1.hasEventListener = true 
_sprite2.hasEventListener = true 

_eventListeners.removeAll() invoked 

_sprite1.hasEventListener = false 
_sprite2.hasEventListener = false 
+0

+1 ...唯一的缺點是addEventListener語法與自然形式不同(您需要先傳遞EventDispatcher對象)... – NemoStein 2011-03-10 14:03:12

+1

@NemoStein將此addEventListener()視爲registerEventDispatcher()相反,你會意識到解析IEventDispatcher對象不是一個缺點,而是方法的要點。但從乍一看,我可以理解爲什麼這個'addEventListener()'方法顯示爲它的同義對象'IEventDispatcher.addEventListener'的格式錯誤版本。我只是給它起了同樣的名字,因爲這些方法是相似的,但不完全一樣。 – Taurayi 2011-03-10 18:47:21