when Event.ADDED_TO_STAGE is call?

227 Views Asked by At

I have this code :

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

when is this event called ? I'm working on a example project of StageVideo but it isn't easy. I'm working on flash pro.

2

There are 2 best solutions below

0
SushiHangover On BEST ANSWER

Event.ADDED_TO_STAGE is called whenever addChild() or addChildAt() is called.

In order to make sure that the stage and parent are available within the object that is being added to the stage that you add the listener of Event.ADDED_TO_STAGE within the object's constructor and then when that object is added to the stage, its Event.ADDED_TO_STAGE listener will be fired and the stage and parent of that object will be available.

Example:

package {

import flash.display.Sprite;

public class Main extends Sprite {

    public function Main() {
        var textField:ChildTextField = new ChildTextField();
        textField.text = "Hello StackOverflow";
        addChild(textField);
    }
}
}

import flash.events.Event;
import flash.text.TextField;

class ChildTextField extends TextField {
    public function ChildTextField() {
        trace("(Stage (Before addChild):" + stage);
        trace("ChildTextField Parent (Before addChild): " + this.parent);
        addEventListener(Event.ADDED_TO_STAGE, initWhenAddedToStage);
    }

    function initWhenAddedToStage(e:Event):void {
        trace("Stage (After addChild): " + stage);
        trace("ChildTextField (After addChild): " + this.parent);
    }
}

Output:

[trace] (Stage (Before addChild):null
[trace] ChildTextField Parent (Before addChild): null
[trace] Stage (After addChild): [object Stage]
[trace] ChildTextField (After addChild): [object Main]
0
akmozo On

As indicated by its name, the addedToStage event is

Dispatched when a display object is added to the on stage display list, either directly or through the addition of a sub tree in which the display object is contained. The following methods trigger this event: DisplayObjectContainer.addChild(), DisplayObjectContainer.addChildAt().

Hope that can help.