mx.utils.Delegate是好用哦

      Flash&AS2 2006-4-4 14:49
一直听说Delegate不过我没用过,今天看actionscript.com上面一个文章顿时想用用这个冬冬了,大家看代码:
//第一桢
new Billows(_root);
//类
class Billows {
  public function Billows(root:MovieClip) {
var self = this;
    root.onEnterFrame = function{
self.myOnEnterFrame();
}
  }
  public function myOnEnterFrame() {
    trace("onEnterFrame");
  }
}
以前要调用onEnterFrame里的myOnEnterFrame要在外面定义一个var self = this,然后再在onEnterFrame里用self来指向Billows这个类,这样的一个缺点是很容易忘了这个内部的指向改变的问题,尤其对初学者来说更是个大问题,现在只要改成这样就可以了:)
//第一桢
new Billows(_root);
//类
import mx.utils.Delegate;
class Billows {
  public function Billows(root:MovieClip) {
    root.onEnterFrame = Delegate.create(this, myOnEnterFrame);
  }
  public function myOnEnterFrame() {
    trace("onEnterFrame");
  }
}
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}