MovieClip.prototype.listen=function(target){
if(this.isHiting==undefined){
this.isHiting=this.hitTest(target);
}
if(this.hitTest(target)){
if(this.isHiting==false){
this.broadcastMessage("onHitTest",this,target);//广播事件,给事件传递this和target两个参数
}
this.isHiting=true;
}else{
this.isHiting=false;
}
};//为MovieClip添加域成员listen成员,用于监视当前对象与目标是否碰撞
MovieClip.prototype.watch=function(target){
this.timer=setInterval(this,"listen",50,target);
};//以每50毫秒检测一次的速度来检测是否碰撞
MovieClip.prototype.unWatch=function(){
clearInterval(this.timer);
};//停止对对象的监视
ASBroadcaster.initialize(MovieClip.prototype);//初始化MovieClip原型为事件源
//下面是调用的示例
//假设有两个MovieClip,左边ball,右边wall,让ball不断往wall移动,同时监视wall,一旦击中触发事件onHitTest
ball.onEnterFrame=function(){
this._x+=5;
};//让ball不断往右方移动工
myListener=newObject();
myListener.onHitTest=function(source,target){
trace("The"+source._name+"hit"+target._name+".");
};
ball.addListener(myListener);//创建监听员并注册给ball
ball.watch(wall);//让ball监视wall