Hi everyone,
I know that there have been questions asked about garbage collection and memory management and after hours of searching through the internet I frustratingly haven’t been able to find a solution to my problem.
So here it is:
I have a function that reads an XML files and creates thumbnail instances of a class. The class contains the usual information as well as two listeners for MOUSE_OVER and MOUSE_OUT. The thumbnails are movieclips and are placed in a sprite container/holder. The container/holder is created with code:
var containerProjects:Sprite = new Sprite(); //Creates sprite to contain projects
spriteGallery.addChild(containerProjects); //Adds container to main container
I can only remove the movieclips from the container but I have no idea how to remove the listeners and set the thumbnails to null or delete them so that they can be garbage collected.
Here is my function that creates the thumbnails:
function fileLoaded(event:Event):void {
urlLoader.removeEventListener(Event.COMPLETE, fileLoaded);
myXML = XML(event.target.data);
projectList = myXML.children();
//Loops through XML
for (var i:int=0; i<projectList.length(); i++) {
var projectThumb = new Thumbnail(projectList*.@SImage, i); //Creates instance of thumbnail class
var projectMC:MovieClip = new MovieClip(); //Creates an empty movieclip
projectMC.addChild(projectThumb); //Adds the thumbnail instance to the empty movieclip
projectMC.addEventListener(MouseEvent.CLICK,projectThumbnailOnClick, false, 0, true); //Adds a click listener to the thumbnail
projectMC.ID = i; //Stores ID for the thumbnail movieclip
projectMC.LImage = projectList*.@LImage; //Stores large image URL for the thumbnail movieclip
projectMC.Title = projectList*.@Title; //Stores title for the thumbnail movieclip
projectMC.Desc = projectList*.@Desc; //Stores desc for the thumbnail movieclip
projectMC.Vid = projectList*.@Vid; //Stores vid URL for the thumbnail movieclip
projectMC.x = 100; //Thumbnail x position
projectMC.y = i*110+65; //Thumbnail y position (below last thumbnail)
containerProjects.addChild(projectMC); //Adds thumbnail to container
}
}
Here is the code to remove the thumbnails:
function clearProjectsContainer():void{
while (containerProjects.numChildren)
containerProjects.removeChildAt(0);
}
Here is my thumbnail class
package {
import flash.display.Sprite;
import fl.containers.UILoader;
import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;
public class Thumbnail extends Sprite {
private var _thumbImage:String;
private var _id:int;
private var _loader:UILoader;
function Thumbnail(thumbImage:String, thumbNo:int):void {
_thumbImage = thumbImage;
_id = thumbNo;
drawLoader();
addEventListener(MouseEvent.MOUSE_OVER,onOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT,onOut, false, 0, true);
}
//This makes the container of the thumbnail a little smaller than gray box
private function drawLoader():void {
_loader = new UILoader();
_loader.source = _thumbImage;
_loader.buttonMode = true;
_loader.x = -50;
_loader.y = -50;
addChild(_loader);
}
private function onOver(event:MouseEvent):void {
TweenLite.to(this, 1, {scaleX:1, scaleY:1, alpha:1, ease:Elastic.easeOut});
}
private function onOut(event:MouseEvent):void {
TweenLite.to(this, 1, {scaleX:0.9, scaleY:0.9, alpha:0.5, ease:Elastic.easeOut});
}
}
}
I need to remove the thumbnails because I have a set of buttons that load different XML files and create new thumbnails in the same containers/holders.
Any help would be greatly appreciated.