Event.ADDED_TO_STAGE 什么时候来电?

when Event.ADDED_TO_STAGE is call?

我有这个代码:

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

此事件何时调用?我正在研究 example project of StageVideo 但这并不容易。我正在开发 Flash 专业版。

如其名称所示,addedToStage 事件是

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().

希望能帮到你。

每当调用 addChild()addChildAt() 时,都会调用

Event.ADDED_TO_STAGE

为了确保阶段和父对象在被添加到阶段的对象中可用,您在对象的构造函数中添加 Event.ADDED_TO_STAGE 的侦听器,然后将该对象添加到阶段,它的 Event.ADDED_TO_STAGE 侦听器将被触发,并且该对象的阶段和父对象将可用。

示例:

package {

import flash.display.Sprite;

public class Main extends Sprite {

    public function Main() {
        var textField:ChildTextField = new ChildTextField();
        textField.text = "Hello Whosebug";
        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);
    }
}

输出:

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