2011-08-26
ActionScript3.0覚書 イベントハンドラに引数を与える
すぐに忘れてしまうのでメモ。
同様の処理をさせたいのに、いちいち別のFunctionを作るのはメンドー!なときのやり方。
以下は、左ボタン・右ボタンで、同じMovieClipを左右に動かしたいときの方法。
moveHandlerの引数に、移動距離を与えて左右に動かすようにしている。
// 呼出元
LeftButton.addEventListener(MouseEvent.CLICK, moveHandler(-10));
RightButton.addEventListener(MouseEvent.CLICK, moveHandler(10));
//呼出先
function moveHandler(x_pos:Number = 0):Function {
return function(e:MouseEvent):void {
// MovieClipを左右に動かす
mc.x += x_pos;
}
}