Search code examples
actionscript-3flash

removin event listener


I am adding the event listener to 25 movieclips. I want to remove all the event listeners from all of them if one of them is used.

var myMvc:movieclip; 
myMvc.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent)
{
   tasiSuruklemeyeBasla(e,myMvc,1,1);
   IEventDispatcher(e.currentTarget).removeEventListener(e.type,argu‌​ments.callee);
});

Solution

  • What you are doing here is creating a separate anonymous function object for each of the listeners. That doesn't make much sense, since the code of the event handler is identical for all of the listeners. Perhaps you are trying to pass some data along with each of event handlers to be able to tell which of your clips has been triggered. There are plenty of better ways to achieve this without creating a separate handler for each listener.

    Anyways, to be able to remove listeners at any time, you have to hold references to your handlers somewhere. For instance, you may declare an event handler function, and pass its reference to all of your listeners (you'd better declare it in a document class or something, but it will work for timeline code either).

    var eventHandler:Function = function(e:MouseEvent):void
    {
       var target:MovieClip = e.target as MovieClip;
       tasiSuruklemeyeBasla(e,target,1,1);
       target.removeEventListener(e.type,argu‌​ments.callee);
    };
    
    myMvc.addEventListener(MouseEvent.MOUSE_DOWN, eventHandler);
    
    // you can do that any time
    myMvc.removeEventListener(MouseEvent.MOUSE_DOWN, eventHandler);