What does "if (stage) init ();" mean mean in ActionScript?

I create my first AS3 using FlashDevelop, and I do not 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 it mean if (stage) init();? What is Event.ADDED_TO_STAGE? Why remove the listener in init()?

+5
source share
1 answer

The main class usually represents the class document → class, which is put on the scene (the root of the display tree) as the first. This means that in the constructor (main function) you already have access to the scene.

if(stage) init();

actually means that if stage! = null, run initialization.

null ?
swf swf. , ( ..), ( ), . :

var mc:MovieClip = new MovieClip();//mc.stage == null
stage.addChild(mc);//mc.stage != null

, ADDED_TO_STAGE, , , . , .

(), , , , .

+9

All Articles