removeEventListenerの必要性によっていくつか方法があります。
removeEventListener不可。
ActionScript
1button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
2 func(e, 'arg');
3});
イベント発生直後にremoveEventListener。
ActionScript
1button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
2 func(e, 'arg');
3 button.removeEventListener(MouseEvent.CLICK, arguments.callee);
4});
初回イベント発生以降に、任意の場所からremoveEventListener可。
ActionScript
1private var handler;
2
3button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
4 handler = arguments.callee;
5 func(e, 'arg');
6});
7
8public function forRemoveEventListener() {
9 button.removeEventListener(MouseEvent.CLICK, handler);
10}
但しこれらは、イベントが発生するまでremoveEventListenerしなくていい場合です。
一度addEventListenerしたイベントを、条件によってイベント未発生のままremoveEventListenerする必要がある場合、Eventオブジェクトを拡張して自前でdispatchEventする必要があります。
Eventオブジェクトの拡張が面倒な場合の裏技。
1dispatcheventwonderfl.php
手間を惜しまずEventオブジェクトを拡張。(使い回しが効き、引数以外の目的で再拡張できます。)
http://blog.project-nya.jp/760
ただ、dispatchEventで任意の引数を渡す最後の2つがしたくなるケースでは、何らかの管理データとインスタンスが1対1で紐づいていたりと、必ずしも引数で渡す必要がないことも多い気がします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/06 14:57