Skip navigation
matt0400@aol.com
Currently Being Moderated

Removing simple MC from memory

May 4, 2012 5:22 PM

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

 
Replies
  • Currently Being Moderated
    May 4, 2012 5:55 PM   in reply to matt0400@aol.com

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 4, 2012 6:58 PM   in reply to matt0400@aol.com

    As I said, it is all hearsay to me, but the fourth result in that search I mentioned might have something to do with what you are seeing.  The intro might just be big enough that the GC decides it shouldn't be scrapped

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points