actionscript-3stagevideo

when Event.ADDED_TO_STAGE is call?


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.


Solution

  • 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]