package
{
import flash.display.InteractiveObject;
import flash.events.MouseEvent;
import flash.utils.getTimer;
/**
* 双击工具(不使用Flash源生的方式,源生的方式是:第二下up时才触发,而这种方式是,第二次down时就触发)
* http://blog.csdn.net/linjf520/article/details/9314095
* @author jave.lin
* @date 2013-7-2
*/
public class DoubleClickUtil{
private static const DOUBLE_CLICK_INTNERVAL:int = 500;
private static var before_double_click_time:int = -1;
private static var isDoubleClick:Boolean = false;
private static var lastObj:InteractiveObject;
public static function registerDoubleClick(obj:InteractiveObject):void{
obj.addEventListener(MouseEvent.CLICK, onClick, false, int.MAX_VALUE);
obj.addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, int.MAX_VALUE);
// obj.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick, false, int.MAX_VALUE);
obj.doubleClickEnabled = false;
}
public static function unRegisterDoubleClick(obj:InteractiveObject):void{
obj.removeEventListener(MouseEvent.CLICK, onClick, false);
obj.removeEventListener(MouseEvent.MOUSE_DOWN, onDown, false);
}
// private static function onDoubleClick(e:MouseEvent):void{
// trace("onDoubleClick");
// }
private static function onDown(e:MouseEvent):void{
// trace("onDown");
var timer:int = getTimer();
if(lastObj == e.currentTarget && before_double_click_time != -1){
// trace("minus:" + (timer - before_double_click_time));
// trace("can double?:" + ((timer - before_double_click_time) <= DOUBLE_CLICK_INTNERVAL));
if((timer - before_double_click_time) <= DOUBLE_CLICK_INTNERVAL){
isDoubleClick = true;
lastObj.dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK));
e.stopPropagation();
e.stopImmediatePropagation();
e.preventDefault();
}else{
isDoubleClick = false;
}
before_double_click_time = -1;
lastObj = null;
}else{
if(before_double_click_time == -1){
before_double_click_time = timer;
}
}
lastObj = e.currentTarget as InteractiveObject;
}
private static function onClick(e:MouseEvent):void{
if(isDoubleClick){
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
isDoubleClick = false;
return;
}
// trace("onClick");
}
}
}
AS3 双击管理工具
最新推荐文章于 2014-12-23 14:19:56 发布