As we cannot use “root” or “stage” when we just new a child (before we addChild it to the displayList), we had to use the two ways:
- trigger the initHandler with ADD_TO_STAGE:
// constructor of some child
public function SomeChild()
{
this.addEventListener(Events.ADD_TO_STAGE, initHandler);
}
private function initHandler(event:Event):void
{
// Make the listener disable so that it won't
// be triggered for more than once.
this.removeEventListener(Event.ADD_TO_STAGE, initHandler);
// the main logic
}
- call the init function from outside:
someChild = new SomeChild();
// a public function in the class "SomeChild"
someChild.init();
Comparing these two ways, which one do you prefer? Or is there any other possibilities?