Hey guys, I have what seems like should be a simple way to add/remove a MovieClip from memory. My game has an animated intro MovieClip, set to export from the library. I can't get this intro MC to be GC'd and I don't know why. It's adding around 40 MBs alone to my total memory usage.
So I have 2 Document class-level instances:
var curtain:Curtains = new Curtains(); //my Transition MC, moves the game between screens
var currentScreen:MovieClip; //stores whatever the current screen is. Gets swapped out when user changes screens.
When the user clicks a button in my main menu, I call a function in my Document Class that adds a transition MovieClip(called "curtain") to the screen. I pass this function the name of a function to call when the transition is done animating. For example, when the user clicks my New Game button, I'll call: "addTransition(loadIntro);" Passing it the function to call when its done
public function addTransition(callThisFunction:Function):void {
curtain.addEventListener("closed", callThisFunction, false, 0, true);
//position and add curtain
curtain.x=stage.stageWidth * .5;
curtain.y=stage.stageHeight * .5;
addChild(curtain);
curtain.gotoAndPlay(2);
}
Then the addTransition function sets up an event listener on the "curtain" transition. This will call the loadIntro function when my curtain MC dispatches the "closed" custom event in its timeline.
So, loadIntro does what it sounds like, creates an instance of the Intro class (linked from the library) and adds it to the screen. Then I make a reference to the Intro class instance ("currentScreen") to store it globally on the class-level.
public function loadIntro(e:Event):void {
removeChild(currentScreen);
var intro:Intro = new Intro();
intro.x=stage.stageWidth*.5;
intro.y=stage.stageHeight*.5;
addChild(intro);
curtain.removeEventListener("closed", loadIntro);
removeChild(curtain);
currentScreen=intro;
intro = null;
currentScreen.gotoAndPlay(2);
currentScreen.addEventListener("intro done", startTutorial, false, 0, true);
}
Now, when the user is done watching (or skips) the intro movie, the startTutorial function is called. This function is SUPPOSED to remove the intro (stored in currentScreen) from the screen and from memory. Doesn't work.
public function startTutorial(e:Event):void {
if (this.contains(currentScreen)) {
trace("remove intro listener!");
currentScreen.removeEventListener("intro done", startTutorial);
currentScreen.stop();
removeChild(currentScreen);
currentScreen = null;
trace(currentScreen);
}
curtain.removeEventListener("closed", startTutorial);
//adds on top of all other elements
addChild(curtain);
curtain.gotoAndPlay("open");
}
I know this is a lot to take in, so I'm very grateful for any help. This is really holding up a project that I've poured 300 hours into.
Thanks in advance
I don't know that you can force garbage collection to happen, and there may be circumstances where objects are too large for GC to decide they should be. But I only speak from hearsay of others who I have to assume are experts. Try Googling "AS3 force gc" and see what you can find that might offer options if there are any for what you are designing. The first four results (especially the fourth) might help explain the problem you face.
Hi Ned, thanks for the response. I do know that you can't force GC in the release player, although you can in the Debug player. I can see that GC is running, because my file will go down by a MB or 2 every once in a while. But as I flip through my intro and menu screens the total memory gets up over 100 MBs!
The file starts at 28 MB at launch, easily gets up over 100 MB, and goes from 28 to 65 just by playing the 1 "intro" movieclip... I don't get it.
North America
Europe, Middle East and Africa
Asia Pacific