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    3 Comments

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: ,