views:

222

answers:

1

I'm creating my first as3 with flashdevelop I don't understand what the instructions mean:

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 if (stage) init(); means ? What is Event.ADDED_TO_STAGE ? Why remove listener in init ?

+5  A: 

Main class is usually a document class -> class that is put to stage (root of display tree) as first. That means in constructor (Main function) you already have access to stage.

if(stage) init();

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

why test for null in document class?
If your swf get's wrapped into another swf. Your Main function will not have access to stage yet, because only sprites (movie clips, etc) that are on display tree (on stage) have access to stage.
Like this:

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

So by adding a listener to ADDED_TO_STAGE you are waiting until you actually have access to stage, and then init it. You remove the listener right away because you don't need it anymore.

This is a common situation in document (main) class, because you need stage to add your menu, intro, whatever to stage, so it is visible.

Antriel
It might be worth pointing out that this is often not necessary. If the class in question needs to make, say, a root-level key event listener, then it will need a stage reference. But if your class doesn't actually need a stage reference for anything, then there's no particular reason for this kind of code (except perhaps as a kind of lazy initialization).
fenomas
@fenomas true, but in my experience I have never seen a main class that doesn't need reference to stage. You always need to add something to stage, since there is no other way to show something.
Antriel
@Antriel: In general a class should add its display components to itself, so it can be reused in other contexts. If you're adding children directly the stage, you're using a de facto global variable.
fenomas
Thanks a lot, that's really a great answer especially as I couldn't find such kind of clear explanation anywhere searching on google.
This question is such a life saver! I'm still, even after you (Antriel) answer, not totally sure why to use this. Is there any way how to find out if I've to use it? Like, any errors if i initialize my stuff before I've the access to stage? Or what else could happen if i initialize my stuff before I've access to stage.Fenomas, by adding a display components to class itself you mean:addChild(); ? With out the stage reference before addChild()?
Richards
You don't need to use it usually, because Main class always have access to stage. If it won't have, stage will be null and therefore using stage will result in runtime exception. But you won't really have any kind of error if you don't use stage in your init function. (we generally use stage to set scaleMode or something in Main class constructor and that is why FlashDevelop creates this template automatically)
Antriel