关于Event.ADDED_TO_STAGE

一. Event.ADDED_TO_STAGE的作用和使用方法

Event.ADDED_TO_STAGE在使用  addChild()函数,将显示对象添加到舞台时触发。

例如:addChild(my_mc);  该方法add完成后会触发该事件

var my_obj:a_class= new a_class();
addChild(my_obj);

上面的代码中会先触发a_class里的函数,然后因为addChild() 而触发Event.ADDED_TO_STAGE事件。

我们看两组不同的例子   来验证Event.ADDED_TO_STAGE

主文档类:

 1 package {
 2     import flash.display.Sprite;
 3     import flash.events.Event;
 4     public class ats_example extends Sprite {
 5         public function ats_example() {
 6             var child:a_child = new a_child();
 7             addChild(child);
 8         }
 9     }
10 }
a_child类:
 1 package {
 2     import flash.display.Sprite;
 3     import flash.events.Event;
 4     public class a_child extends Sprite {
 5         public function a_child() {
 6             trace("this is the stage: "+stage);
 7             trace("this is my parent: "+this.parent);
 8         }
 9     }
10 }

会发现输出结果为:  

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

当我使用Event.ADDED_TO_STAGE事件来修改一下a_child类:

 1 package {
 2     import flash.display.Sprite;
 3     import flash.events.Event;
 4     public class a_child extends Sprite {
 5         public function a_child() {
 6             addEventListener(Event.ADDED_TO_STAGE, init);
 7         }
 8         function init(e:Event):void {
 9             trace("this is the stage: "+stage);
10             trace("this is my parent: "+this.parent);
11         }
12     }
13 }

会发现输出的结果是:

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

//上述表明虽然执行了a_Child里的构造函数 但是由于不存在addChild()函数的触发 所以init函数并没有触发 而是在文档类中将其添加进舞台,而返回去a_child构造函数类的

init()函数

**************************************************

上面的方法给我提供另一种途径去延缓某个类相关方法的执行

原文地址:https://www.cnblogs.com/hisiqi/p/2714900.html