Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Wednesday, May 7, 2008

AS3: Clearing out loader instance contents

Wednesday, May 7, 2008   

This wasn't very obvious to me at first, so I report my working findings here for the benefit of perhaps someone out there. 

I have a gallery application that uses two instances of a Loader. Instead of creating/destroying the instances, I re-use the instances... but for me to make swapping galleries (full swap), I needed a way to clear out the images in the Loader instances... essentially creating a reset method in my gallery class. 

Well... there isn't an obvious clear() anywhere, and when you inspect the contents of a Loader instance (that has been loaded successfully), you'll notice it's BitmapData. Okay... so we need a way of throwing out that type of data. We have the dispose() method.

However, depending on where one might be in the cycling of images (fading), you might have a Loader that doesn't presently contain BitmapData. This might be when you're only one image into the whole cycle of images... trying to clear out both at once in the reset would throw an error.

The code below reflects usage that works for me right now. 


var li = child2.loader.contentLoaderInfo;
if( child2.loader.content != null )
{
    if(li.childAllowsParent && li.content is Bitmap)
    {
        (li.content as Bitmap).bitmapData.dispose();
    }
}

var li2 = child1.loader.contentLoaderInfo;
if( child1.loader.content != null )
{
    if(li2.childAllowsParent && li2.content is Bitmap)
    {
        (li2.content as Bitmap).bitmapData.dispose(); 
    }
}



So this checks the contents of each of the loaders and then clears the contents out... then I am free to load up the new XML and start my timer, etc. It works... before I was getting mucked up cycling after a gallery switch.

Labels: ,

 
 Return to the main page
Comments:

There are currently 3 Comments:

Anonymous Anonymous said...
“loader.unload() doesn't dispose of the bitmapData automatically?”
 
Anonymous Anonymous said...
June 25, 2008 10:29 AM
“I thought a loader was only allowed to have 1 child?

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html

"a Loader object can only have one child display object—the display object that it loads"

Are you putting 2 bitmaps into 1 loader?”
 
Blogger e.dolecki said...
June 25, 2008 10:33 AM
“2nd anon: using 2 loaders. wanted to clear out each.

1st anon: the dispose() worked, never tried unload() ;)”
 
 Leave a comment