Understand ADDED_TO_STAGE event

Emanuele Feronato Actionscript 3, Flash

You are about to meet an event that is not used as it would deserve, because – sad but true – programmers do not use it that much.

Let me give ADDED_TO_STAGE event its moment of fortune and fame, and you will never ever live without it.

First, let me say the event is triggered when an object is added to stage, that is when you execute an addChild, this way:

addChild(my_mc);

When the object is successfully placed on the stage, Event.ADDED_TO_STAGE event will be fired.

Ok, but what’s the point?

Well, in AS3, things that have access to stage are not on stage unless you put them there using addChild.

So a script like this

will initialize the my_obj object before it’s physically placed on the stage.

So any line of code inside the a_class function inside the a_class class will be executed before my_obj is on the stage.

What’s the point?

Let’s see this script:

where a_child.as is:

The output is:

this is the stage: null
this is my parent: null

and this can draw you mad, thinking about an AS3 bug… but it’s just a malpractice.

Try this one instead:

and you will see the light:

this is the stage: [object Stage] this is my parent: [object ats_example]

I admit it… I lost an hour of my life trying to understand those null values…

Comments 15

  1. Bivis

    This event is really usefully.

    LOL! I wasted 3 hours of my life to solve the movement for a player…
    This event is good to add key listener for move a object by keyboard, when its not on the main class…

    Nice post!

  2. Atef OUNI

    Creating a new AS3 project with FlashDevelop generates this code for the main.as file:

    package
    {
    import flash.display.Sprite;
    import flash.events.Event;

    /**
    * …
    * @author Author
    */
    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);

    }

    }

    }

    You should have a look to this software, it’s very helpful and i think that it’s the best tool for writing actionscript codes.

  3. Quintus

    Rappidly gaining speed in the subject here. But some how ppl dont understand how to gain the right speed in the right amount of time. At first i thought this wasn’t supposed to be written but when i saw a unichorn eat an papaya fruit i gasped. Isn’t there any other way? But no there wasn’t. Cars drive faster when you put it in his highest gear, ofc everyone knows this but still regret it. What i want to point out is: Is there ever going to be a Correct awsner to this? No i dont think so.

  4. Christian

    It’s simple:

    1) 0 is a value just like 1 and 2
    2) when a variable has the value null, it means the variable isn’t referring to any object
    3) void is a method, a method that doesn’t return a value

    P.S. Quintus -> what are you talking about ?

  5. jeanpier

    This event can be very useful, I use it a lot, because that way I can make sure that stage is not null when I add my objects to it.
    Flash Develop add the code automatically when you create new AS3 project.
    Good tip Emanuele, as usual ;)

  6. DeadÑeSS

    it’s very usefull on OOP project.. i have been using since i learned OPP. And i don’t think you realy lost one hours for trying to understand null objects (: you might be exaggerating i think lol… anyway good heading to share (:

  7. John

    Yep, sure is an awesome event. I use it all the time…
    Before I knew about it i used to send the stage as parameter into every class that used it. This is so much easier and cleaner.

  8. Bishibosh

    Hi.. man your tutorial has made me understand about the null objects… but I still have problems loading a swf gallery inside my portfolio… Could you check out my code please?? thanx

    package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    import gs.TweenMax;
    import gs.easing.*;

    public class Galeria extends MovieClip {
    private var __iPhone:DragThrow;
    private var __xmlContent:XMLList;
    private var __imgsToLoad:int= 0;
    private var __imgsLoaded:int= 0;
    private var __loaders:Array= [];
    private var __menuloaders:Array= [];
    private var __menuShowing:Boolean= true;

    // ______________________________________________
    // PUBLIC

    // SETUP MAIN CLASS
    public function Galeria() {
    addEventListener(Event.ADDED_TO_STAGE,setup);
    setup();
    }

    // ______________________________________________
    // PRIVATE

    // SETUP THE MAIN DRAG AND THROW FUNCTIONALITY
    // AND START TO LOAD THE CONTENT XML FILE
    private function setup(e:Event=null):void {
    // setup iPhone drag and throw functionality
    removeEventListener(Event.ADDED_TO_STAGE,setup);
    var _root:MovieClip=parent.parent as MovieClip;
    __iPhone= new DragThrow();
    __iPhone.contentMC= iPhoneMC.holder_mc.content_mc;
    __iPhone.width= 660;
    __iPhone.height= 400;
    __iPhone.counterHolderMC= iPhoneMC.counter_mc;
    __iPhone.counterMC= CircleMC;
    __iPhone.theStage= stage;

    // disable events
    iPhoneMC.shadow_mc.mouseEnabled= false;

    // load xml content
    loadXML(“content.xml”, xmlLoaded);

    // setup stage align and listeners
    stage.align= “TL”;
    stage.scaleMode= “noScale”;
    stage.addEventListener( Event.RESIZE, resizeHandler );
    resizeHandler();
    }

  9. Pingback: AS3 OOP Case Study: Build a Ferris Wheel

Leave a Reply

Your email address will not be published. Required fields are marked *