Event.ADDED_TO_STAGE的本质

我们都知道Event.ADDED_TO_STAGE在使用  addChild()函数,将显示对象添加到舞台时触发:

  addChild(my_mc);  触发该事件

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

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

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

主文档类:


 package {
 import flash.display.Sprite;
 import flash.events.Event;
 public class ats_example extends Sprite {
  public function ats_example() {
   var child:a_child = new a_child();
   addChild(child);
  }
 }
}a_child类:


package {
 import flash.display.Sprite;
 import flash.events.Event;
 public class a_child extends Sprite {
  public function a_child() {
   trace("this is the stage: "+stage);
   trace("this is my parent: "+this.parent);
  }
 }
}  会发现输出结果为:     this is the stage: null   this is my parent: null  当我使用Event.ADDED_TO_STAGE事件来修改一下a_child类:  package {
 import flash.display.Sprite;
 import flash.events.Event;
 public class a_child extends Sprite {
  public function a_child() {
   addEventListener(Event.ADDED_TO_STAGE, init);
  }
  function init(e:Event):void {
   trace("this is the stage: "+stage);
   trace("this is my parent: "+this.parent);
  }
 }
}
 
 
会发现输出的结果是:
   this is the stage: [object Stage]this is my parent: [object ats_example]

//上述表明虽然执行了a_Child里的构造函数  但是由于不存在addChild()函数的触发 

    所以init函数并没有触发   而是在文档类中将其添加进舞台,而返回去a_child构造函数类的 init() 函数

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

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

posted on 2011-06-27 10:36  破阵子 . 如是我闻  阅读(820)  评论(0编辑  收藏  举报

导航