一度しか実行できないEventListener


id:c9katayamaに教えてもらってので無駄に使ってみる。
あるボタン(button)は一度しか出来ないことを保障したいとして、
実行した瞬間にremoveEventListenerしたいとする。
ただし、別のボタン(button2)を押したらリカバーしたいとする。


そんなときはまずはこんなユーティリティを用意しておいてやる、と。
実際に実行されるFunctionを、更にFunctionでデコレートしてやって、
removeEventListenerを差し込んでしまうのがポイント。

public static function addOnceEventListener(dispatcher:EventDispatcher,
	eventType:String,listener:Function):void{
	var f:Function = function(e:Event):void{
		dispatcher.removeEventListener(eventType,f);
		listener(e);
	};
	dispatcher.addEventListener(eventType,f);
}

で、こいつを無駄にリカバーするようにする。
ポイントは一度っきりのイベントを実行するFunctionを準備するFunctionを定義しちゃえーってところ。
(何言ってるかわからないってwww?)
で、そいつを即時実行するのと、更にbutton2が押されたときにも実行すること。

	var once:Function = function():void
	{
		addOnceEventListener(button, MouseEvent.CLICK, function(e:MouseEvent):void
			{
				Alert.show("hoge");
			}
		);
	};
	once.apply();
	button2.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
		{
			once.apply();
		}
	);

でけた。



結論。キメエwwwwwwwwww