删除所有ENTER_FRAME侦听器

时间:2011-03-09 20:54:29

标签: actionscript-3 listeners

有没有办法一次删除所有ENTER_FRAME事件侦听器?

2 个答案:

答案 0 :(得分:3)

只有你一直在仔细跟踪你的听众。事件监听器没有“removeAll”类型的功能,因此有助于在易于管理的地方收集监听器。

答案 1 :(得分: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