[AS3]AS3鼠标双击事件的源代码
AS3.0 增加了对鼠标事件DOUBLE_CLICK的支持,AS3对事件的触发流程为 down,up,click,down,up,doubleclick.
AS3.0 增加了对鼠标事件DOUBLE_CLICK的支持,AS3对事件的触发流程为 down,up,click,down,up,doubleclick.
这带来一个问题:如果我对显示对象同时增加了down,up,click,doubleclick事件,如果双击一次,会依次触发 down,up,click,down,up,click,doubleclick这6个事件。这并不是我期望的结果。(当我们期望一个显示对象可以有单 击,双击,拖拽时就会遇到该问题)
参考网上的一些解决方法,然后优化了一下,做成一个示例如下,备用。基本的解决思路就是不使用原生的click和doubleclick事件,仅仅使用down和up事件,在这两个事件里根据触发事件的时间差值来判断单击,双击和拖拽。
- package {
- import com.greensock.TweenLite;
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import flash.events.MouseEvent;
- import flash.utils.getTimer;
- public class test extends Sprite {
- private var _rect:Sprite;
- public function test() {
- trace("hello world");
- // this.stage.addEventListener(MouseEvent.CLICK, __onTraceClick);
- drawRect();
- _rect.addEventListener(MouseEvent.ROLL_OVER, __onOver);
- _rect.addEventListener(MouseEvent.ROLL_OUT, __onOut);
- _rect.addEventListener(MouseEvent.MOUSE_DOWN, __onDown);
- }
- private function __onOver(evt:MouseEvent):void {
- //trace("over");
- }
- private function __onOut(evt:MouseEvent):void {
- //trace("out");
- }
- private var _preDownTime:int = 0;
- private function __onDown(evt:MouseEvent):void {
- trace("down");
- TweenLite.killDelayedCallsTo(doClick);
- _rect.addEventListener(MouseEvent.MOUSE_UP, __onUp);
- _rect.startDrag();
- _preDownTime = getTimer();
- }
- private const UP_INTER:int = 300;
- private var _preUpTime:int = 0;
- private function __onUp(evt:MouseEvent):void {
- trace("up");
- _rect.removeEventListener(MouseEvent.MOUSE_UP, __onUp);
- _rect.stopDrag();
- var now:int = getTimer();
- if((now - _preUpTime) < UP_INTER) {
- trace("- cuplayer.com ---double click");
- } else if((now - _preDownTime) < UP_INTER) {
- TweenLite.delayedCall(0.2, doClick);
- } else {
- trace("drag");
- }
- _preUpTime = now;
- }
- private function doClick():void {
- trace("----click");
- }
- private function __onTraceClick(evt:MouseEvent):void {
- var str:String = "";
- trace(printTrace(evt.target as DisplayObjectContainer));
- }
- private function printTrace(sp:DisplayObjectContainer):String {
- if(null == sp) {
- return "";
- } else {
- return sp.toString() + "/" + printTrace(sp.parent);
- }
- }
- private function drawRect():void {
- var rect:Sprite = new Sprite();
- // CuPlayer.com 提示 red rect, starting at point 50, 50
- rect.graphics.beginFill(0xFF0000);
- rect.graphics.moveTo(50, 50);
- rect.graphics.lineTo(150,50);
- rect.graphics.lineTo(150,150);
- rect.graphics.lineTo(50,150);
- rect.graphics.endFill();
- _rect = rect;
- this.addChild(_rect);
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...