# Why does this create more than one custom class?

**URL:** <https://forum.kirupa.com/t/why-does-this-create-more-than-one-custom-class/329859>\
**Category:** flash\
**Created:** [October 25, 2012, 5:54pm UTC](https://forum.kirupa.com/t/why-does-this-create-more-than-one-custom-class/329859 "2012-10-25T17:54:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [October 25, 2012, 5:54pm UTC](https://forum.kirupa.com/t/why-does-this-create-more-than-one-custom-class/329859/1 "2012-10-25T17:54:49Z")

</div>

I will try to summarize this as much as possible, based on what I believe I have isolated as the reason for this behavior.

I have a solitaire game. The main class, Solitaire, calls an instance of Console, which manages buttons and a timer. The way I originally had it coded, I actually ended up with TWO consoles. That looked like this (with much snipping):

```php
package {    
     public class Solitaire extends Sprite {
          private var console:Console;
          public function Solitaire():void {
                    addEventListener(Event.ADDED_TO_STAGE, init);
          }
          private function init(e:Event):void {
                    console = new Console();
                    addChild(console);
                    ...
                    // do stuff with stage
          }
    }
}

package {
    public class Console extends Sprite {
        public function Console():void {
            trace("Console");
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event) {
            // do stuff with stage
        }
    }
}

```

So this:

```php
var solitaire:Solitaire = new Solitiare();
addChild(solitaire);

```

…results in this trace:

```php
Console
Console

```

Certainly I don’t want two instances of Console. Not knowing what caused this, I experimented with removing the ADDED\_TO\_STAGE event listener in Console. The result is in fact only one instance of Console, which is good, EXCEPT that now I don’t have access to the stage within Console, and I need to pass stage-related values to it from Solitaire.

I assume there is a better approach. So my questions are two:[LIST=1]  
[_]Why does the ADDED\_TO\_STAGE event listener on Console result in two instances of it being instantiated?  
[_]Is there a better way of avoiding duplicating Console without getting rid of the ADDED\_TO\_STAGE listener on Console? Since I need access to the stage.  
[/LIST]Thank you!
