Alright! So, I’d like some clarification on garbage collection.
If I remove all references to an display object (container sprite), all of its children will be collected as well, given that none of them are tied past the parent?
Game → Display Object → Children
Display Object has reference to Game, but no children have reference to game.
So if I remove all references with Display Object, will the Children be collected as well?
derps a little
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
private var game:Game;
private var menu:Menu = new Menu();
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.RIGHT_CLICK, doNothing);
addChild(menu);
}
private function doNothing(e:MouseEvent):void
{
}
public function startGame():void
{
removeChild(menu);
menu = null;
game = new Game();
addChild(game);
}
public function startMenu():void
{
removeChild(game);
game = null;
menu = new Menu();
addChild(menu);
}
}
}
So will I have to go through and manually remove all listeners of the children of Game, or will the children be scrapped as well?
I’m pretty good with keeping my code clean and removing listeners and such when they’re not in use, but when It’s time to throw everything out (leave the game completely) it would just be easier to cut one string than go through and snip snip the remaining ones.