ダブルクリックとシングルクリックの判定
もっとシンプルかスマートに書く方法あるんでしょうか?
/**
* Copyright dkgkAs ( http://wonderfl.net/user/dkgkAs )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eH9n
*/
//もっとシンプルかスマートに書く方法あるんでしょうか?
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
public class ClickTest extends Sprite
{
private var _delay:int = 200;
private var _isDoubleClicked:Boolean = false;
private var _tf:TextField = new TextField();
public function ClickTest()
{
_tf.text = "ステージをクリック or ダブルクリック\n";
_tf.width = 300;
_tf.autoSize = TextFieldAutoSize.LEFT;
addChild(_tf);
_init();
}
private function _init():void
{
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.CLICK, _singleClickHandler);
stage.addEventListener(MouseEvent.DOUBLE_CLICK, _doubleClickHandler);
}
private function _singleClickHandler(event:MouseEvent):void
{
_isDoubleClicked = false;
var timer:Timer = new Timer(_delay, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function (event:TimerEvent):void
{
event.target.removeEventListener(event.type, arguments.callee);
_mouseClickExecutor(_isDoubleClicked);
});
timer.start();
}
private function _doubleClickHandler(event:MouseEvent):void
{
_isDoubleClicked = true;
}
private function _mouseClickExecutor(isDoubleClicked:Boolean):void
{
if (isDoubleClicked)
{
_tf.appendText("doubleClick\n");
}
else
{
_tf.appendText("singleClick\n");
}
_tf.appendText("------------\n");
}
}
}