Skip to main content
Inspiring
July 2, 2010
Question

Adding or Removing Event Listeners all at once?

  • July 2, 2010
  • 1 reply
  • 439 views

Hello!

I'm trying to do something that I'm not sure if it's possible.


I have to add or remove multiple listeners that will trigger the same three methods (onComplete, onError and showProgress).

I was trying to do something like this:

Start with:

addListeners(bkgLoader); //that runs the following method:

private function addListeners(e:Loader):void {
     e.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
     e.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
     e.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);
}

The problem is that I just can't reutilize this because onComplete and onError I'm forced to "hardcode" bkgLoader again, for example:

private function onError (e:IOErrorEvent):void {

     removeListeners(bkgLoader); //bkgLoader hardcoded here.

     trace("IOError occured");

}

Is there anyway to solve this? It seems that after passing through the addListeners method, the loader instance gets lost.

Thanks!

This topic has been closed for replies.

1 reply

Darshan_Rane
Inspiring
July 2, 2010

Why dont you write it directly

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);

newToAS3Author
Inspiring
July 2, 2010

cause I have more than one loader.