arguments.callee
を使う。
Docs[argument]
arguments オブジェクトは、関数の引数の保存や、引数へのアクセスに使用されます。関数の本体に含まれる場合、arguments オブジェクトにはローカルの arguments 変数を使用してアクセスできます。
var count:Number = 0
addEventListener(Event.ENTER_FRAME, function():void {
count++
if(count > 10){
removeEventListener(Event.ENTER_FRAME,arguments.callee);
}
}
以下、使ってみたsample
クロージャの中でイベントを削除する - wonderfl build flash online
package {
import flash.geom.Point;
import flash.events.Event;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.display.Sprite;
[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "000000")]
public class Line extends Sprite {
public function Line() {
// write as3 code here..
init();
}
private function init():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
}
private function mouseDownHandler(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
private function mouseUpHandler(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
private function mouseMoveHandler(e:MouseEvent):void
{
drawLines(mouseX,mouseY);
}
private function drawLines(startX:Number,startY:Number):void
{
var line:Shape = new Shape();
var col:uint = Math.random() * 0xFFFFFF;
var count:Number = 0;
addChild(line);
line.x = startX;
line.y = startY;
line.graphics.lineStyle(1,col);
line.graphics.moveTo(20,0);
line.addEventListener(Event.ENTER_FRAME,
function():void{
count+= 0.1;
var px:Number = 20 * (Math.cos(count) + count * Math.sin(count));
var py:Number = 20 * (Math.sin(count) - count * Math.cos(count));
line.graphics.lineTo(px,py);
line.alpha -= 0.005;
if(count > 10){
removeChild(line);
line.removeEventListener(Event.ENTER_FRAME,arguments.callee);
}
});
}
}
}