2004/11/05 | 关于Delegate的用法技巧
类别(AS3) | 评论(2) | 阅读(1673) | 发表于 09:32
一、如果需要移除监听事件中的委派切记不可以使用匿名类:

myButton.addEventListener("click",mx.utils.Delegate.create(this,onClick));

这样使用匿名类来传入,无法移除

myButton.removeEventListener("click",mx.utils.Delegate.create(this.onClick));

正确做法是保存引用

var myDelegate=mx.utils.Delegate.create(this.onClick);

myButton.addEventListener("click",myDelegate);

myButton.removeEventListener("click",myDelegate);

二、内置类事件的委派可以使用Delegate
我们一般的做法是增加owner来指向this路径
例:

var myCam=Camera.get();

Object(myCam).owner=this;

myCam.onStatus=function(info){

   this.owner.onCamStatus;
}

可以写成

var myCam=Camera.get();

myCam.onStauts=mx.utils.Delegate.create(this,onCamStatus);

在onCamStauts中的this指向是正确的!
0

评论Comments

日志分类
首页[33]
AS3[20]
工作日记[10]
个人简介[2]
ASV[1]