Heguemo
Heguemo

Reputation: 1

Hsharma Starling tutorial, is necessary the ADDED_TO_STAGE event?

I'm following this tutorial which is great but I can't understand why he puts an ADDED_TO_STAGE event in the constructor function of every class he creates. If you put that code directly on the constructor function it runs ok. Is there some reason to make it that way?

    package pantallas
    {
        public class Menu extends Sprite
        {
            public function Menu()
            {
                super();
                this.addEventListener(starling.events.Event.ADDED_TO_STAGE, iniciarMenu);
            }   

            private function iniciarMenu (event:Event):void
            {
                trace ("Menú iniciado");
                dibujarPant ();
            }
        }
}

VS

package pantallas
{
    public class Menu extends Sprite
    {
        public function Menu()
        {
            trace ("Menú iniciado");
            dibujarPant ();
        }   
    }
}

Upvotes: 0

Views: 143

Answers (1)

ThanksBro
ThanksBro

Reputation: 911

because, you can ran into some errors if you execute the code, and content is not on stage. This way you are certain.

for example, when DisplayObject is added to stage, your parent and stage properties are not null

Upvotes: 0

Related Questions