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()函数

 

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

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值