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):
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:
var solitaire:Solitaire = new Solitiare();
addChild(solitaire);
…results in this trace:
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!