# Garbage Collector Questions

**URL:** https://forum.kirupa.com/t/garbage-collector-questions/319052
**Category:** flash
**Created:** [February 7, 2011, 7:19pm UTC](https://forum.kirupa.com/t/garbage-collector-questions/319052 "2011-02-07T19:19:53Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Travisism](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Travisism](https://forum.kirupa.com/u/Travisism)
#### Post date: [February 7, 2011, 7:19pm UTC](https://forum.kirupa.com/t/garbage-collector-questions/319052/1 "2011-02-07T19:19:53Z")

</div>

Howdy all,

I’ve got a few questions about flash’s garbage collector. I’m building a game and have all elements in a container sprite called \_gameContainer.

\_gameConatiners contains other sprites such as \_hudContainer which contains more containers such as \_gameoverContainer, \_menuContainer, etc. So there’s a whole hierarchy of containers that are all contained in \_gameContainer which is added directly to the document class (not the stage btw).

So the whole thing should basically be Stage -\> Document Class -\> \_gameContainer -\> etc.

When the player loses or wants to restart the game, I’m removing all event listeners (which are defined as weak, but still rather make sure there’s no lose ends), stopping all music/sounds, removing \_gameContainer from the stage, and then setting \_gameContainer to null. From there I am re-calling my init function which creates everything (\_gameContainer, and evertying inside of it, as well as creating all the standard even listeners).

Am I doing everything upside down? Is this _a_ proper way of restarting a game? Mind you I dont say “the” proper way since I’m sure there’s a hundred different ways to do this.

Also, on a separate note… If I have something such as an enemy. The enemy logic is all contained in the class linked to the movieclip on the stage. Who should be calling add/removechild? Should I be using a factory method that takes care of all this, or should I have the engine create the enemy, and then have the enemy remove itself from stage? Currently I’m using a mix of both, but generally I’ll have a function in the engine/caller add it to stage, then have the class have an ADDED\_TO\_STAGE event listener. When it comes time to remove the class, I have it call it’s own removeself function such as: (\_container is a reference to it’s container as mentioned above such as \_hudContainer)

```php
protected function removeSelf():void
{
    if(_container.contains(this)) {
        _container.removeChild(this);
    }
    _container.parent.parent.dispatchEvent(new Event(Engine.START_GAME));
}

```

Thanks all! As a heads up, I am decently new to both game development, and to some extent AS3, though I’ve been a JavaScript/PHP Developer for near a decade now, so I am comfortable with OOP/development in general.
