关于Delegate的用法技巧

      AS3 2004-11-5 9:32:00
一、如果需要移除监听事件中的委派切记不可以使用匿名类:

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指向是正确的!
标签集:TAGS:
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}