views:

68

answers:

1

Ive been using the loader a lot of upload images into my movieclip. But the only way I know how to do it is load the content, add an event listener, wait for it to finish finish the job in the handler using the reference to the loader like this.

protected function loadImage(imageDir:String):void
        {
             loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.INIT,tilesLoadInit);
            loader.load(new URLRequest(imageDir));
        }
 private function tilesLoadInit e:Event):void {
               sprite = Bitmap(loader.content).bitmapData;
               //load in xml file for map
               xmlToArray();
         }

I am trying to be more light with my code. And I would rather call my loader:Loader class locally in the method instead of making a reference in my class. Is there any way to retrieve that loader object in the Event.INIT parameter of my handler?

Same with Sprites in general. Say I create a button. the user clicks on it and it calls the handler. is there anyway I can retrieve that button, remove it from the stage through the Event parameter instead of creating Class references to remove them.

I want to do this to make it easier on garbage collecting. so instead of creating class references of all my objects. I would rather call them locally so when I remove them from the stage, the garbage collector will take care of them.

+1  A: 

You can use event.currentTarget inside an event listener to get a reference to the object that registered the event listener.

private function localFunc():void
{
  var btn:Button = new Button();
  btn.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void
{
  //e.currentTarget is typed as Object: in order to assign it to a 
  //variable of type Button, you must cast it accordingly.
  var btn:Button = Button(e.currentTarget);
}

In case of loader events, the currentTarget will be the associated LoaderInfo object. LoaderInfo has a loader property that points to the original Loader object.

private function tilesLoadInit(e:Event):void 
{
  var linfo:LoaderInfo = LoaderInfo(e.target);
  var ldr:Loader = linfo.loader;//this is the Loader you wanted
}

The target and currentTarget properties will be same in LoaderInfo events, but can be different in Buttons and other display objects. For example, when you add click event listener to a button and click on a text field that is a child of the button, event.target will be the text field and event.currentTarget will be the button. Basically, target will contain the exact object that triggered the event and currentTarget will be the object with which the currently executing event listener was registered with.


As for the garbage collection, you have to remove the event listener from the corresponding object before garbage collector can sweep it away. Or you can use weak references while adding the event listener by setting the fifth parameter true in the addEventListener method. Garbage collector does not count weak references while checking if an object is eligible for garbage collection.

But if you use weak reference for a local variable, the object might get GC'ed as soon as it goes out of scope (that is when the method returns) if it doesn't have any more references to it, and the event listener might not get called at all : this is not an issue with buttons as you would add them to the display list before returning, thus creating a strong reference to it.


Use Event.COMPLETE instead of Event.INIT to read the content. From livedocs:

complete — Dispatched by the associated LoaderInfo object when the file has completed loading.

init — Dispatched by the associated LoaderInfo object when the properties and methods of the loaded SWF file are accessible. The init event always precedes the complete event.

Amarghosh
It appears to work so far. thanks
numerical25