存档

文章标签 ‘移除’

as3移除匿名监听函数

2009年3月16日 admin 2 条评论

有时候写代码的时候图方便,比如在监听句柄中使用添加监听时的参数,在监听中直接用匿名函数,可以使用arguments.callee获得该匿名监听函数的引用,来移除,举个简单的例子
m_someBtn.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
(e.target).removeEventListener(MouseEvent.CLICK,arguments.callee);

})
arguments.callee 是as2时代就有的东西了,可以方便的引用当前执行的函数,引个例子吧
private var count:int = 1;

public function ArgumentsExample() {
firstFunction(true);
}

public function firstFunction(callSecond:Boolean) {
trace(count + “: firstFunction”);
if(callSecond) {
secondFunction(arguments.callee);
}
else {
trace(”CALLS STOPPED”);
}
}

public function secondFunction(caller:Function) {
trace(count + “: secondFunction\n”);
count++;
caller(false);
}

Popularity: 68% [?]

分类: flash&flex 标签: , ,