Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Understand ADDED_TO_STAGE event

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…

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (35 votes, average: 4.89 out of 5)
Loading...Loading...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 15 comments

  1. Bivis

    on December 3, 2009 at 3:46 am

    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

    on December 3, 2009 at 9:16 am

    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

    on December 3, 2009 at 10:39 am

    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. Kaustav

    on December 3, 2009 at 11:20 am

    yep, i still dont understand the difference between null, void, and 0. lol

  5. Christian

    on December 3, 2009 at 2:28 pm

    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 ?

  6. jeanpier

    on December 3, 2009 at 5:43 pm

    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 ;)

  7. DeadÑeSS

    on December 4, 2009 at 2:30 am

    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 (:

  8. John

    on December 8, 2009 at 7:46 am

    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.

  9. Bishibosh

    on December 14, 2009 at 12:08 am

    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();
    }

  10. Bishibosh

    on December 14, 2009 at 12:34 am

    I solved it!! thanks anyway

  11. AS3 OOP Case Study: Build a Ferris Wheel

    on June 21, 2011 at 3:16 pm

    […] As mentioned in the video here is the explanation of ADDED_TO_STAGE event from Emanuele Feronato. […]

  12. Mohit

    on July 8, 2011 at 12:52 pm

    Thanks it really helped

  13. goku

    on September 17, 2011 at 3:14 pm

    Thanks, This help me a lot!

  14. Sébastien Gicquel - Webdesigner

    on September 29, 2011 at 10:13 am

    Thanks a lot, good tutorial as usual !

    “I admit it… I lost an hour of my life trying to understand those null values…”
    Haha !! I think i’ve lost more than an hour !

  15. anchampala

    on March 25, 2012 at 10:08 am

    you are the MAN!

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×