I'm creating my first AS3 with FlashDevelop and I don't understand the meaning of the instructions in the constructor:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
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);
// entry point
}
}
}
What does if (stage) init(); mean? What is Event.ADDED_TO_STAGE? Why remove listener in init()?
initis called only when your component is on "stage": If you already are, you call it immediately. If you aren't, you hook up a listener for the event that fires when you're put on stage (and then remove it when it's called). - T.J. Crowder