I think the following is a bug, as the documentation states that unload() on LoaderMax does the following: "Removes any content that was loaded ..." however the ContentDisplay classes is still kept in my container in the following example:
I have a _mainLoader LoaderMax containing ImageLoader's. It's a simple gallery with next/previous functionality and when next/previous is called, the LoaderMax flushes it's content and a new image is loaded. However ContentDisplay instances aren't being removed from their container correctly.
Setup of the loader, event handlers etc. - _images is an Array containing strings(url's to the images)
public class SimpleGallery extends Sprite
{
// Images
private var _images:Array;
private var _index:int = -1;
// Container
private var _imageHolder:Sprite;
// Loading
private var _mainLoader:LoaderMax;
public function SimpleGallery($images:Array)
{
_images = $images;
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, destroy, false, 0 , true);
_imageHolder = new Sprite();
addChild(_imageHolder);
setupGallery();
}
private function setupGallery():void
{
_mainLoader = new LoaderMax( { name:"mainLoader", auditSize:true, onChildProgress:handleProgress, onChildComplete:handleComplete, onError:handleErrors } );
loadNext();
}
private function handleProgress(e:LoaderEvent):void
{
trace(this + " load progress: " + int(ImageLoader(e.target).progress * 100) + "%");
}
private function handleComplete(e:LoaderEvent):void
{
trace(this + " Loader " + ImageLoader(e.target).name + " successfully loaded: " + ImageLoader(e.target).url);
dispatchEvent(new Event(Event.CHANGE));
}
private function handleErrors(e:LoaderEvent):void
{
trace(this + " ERROR loading on loader " + ImageLoader(e.target).name + " loading image: " + ImageLoader(e.target).url);
}
clearMainLoader method:
private function clearMainLoader():void
{
_mainLoader.unload();
_mainLoader.empty();
var i:int = _imageHolder.numChildren;
trace(this + " clearMainLoader() found " + i + " children in _imageHolder. Removing them now.");
while (i--)
{
//_imageHolder.removeChildAt(i);
}
}
Now, if i don't uncomment that '_imageHolder.removeChildAt(i);' line the content keeps piling up in _imageHolder.
Next previous methods:
/**
* Loads the next image in the provided array
*/
public function loadNext():void
{
_index++
if (_index == _images.length) _index = 0;
clearMainLoader();
_mainLoader.append(new ImageLoader(_images[_index], { container:_imageHolder } ));
_mainLoader.load();
}
/**
* Loads the previous image in the provided array
*/
public function loadPrevious():void
{
_index--
if (_index == -1) _index = _images.length - 1;
clearMainLoader();
_mainLoader.append(new ImageLoader(_images[_index], { container:_imageHolder } ));
_mainLoader.load();
}