cocos creator 检测双击 、长按的实现

`cc.Class({
extends: cc.Component,

properties: {
    holdTimeEclipse:0,//用来检测长按
    holdClick:false,
    doubleTimeEclipse:0,
},
// use this for initialization
onLoad: function () {
    //touchstart   touchend
    this.node.on('touchstart',function(event){
        this.holdClick=true;
        this.holdTimeEclipse=0;
    },this);
    this.node.on('touchend',function(event){
        this.holdClick=false;
        if(this.holdTimeEclipse>=30)
        {
            //console.log('long');
            this.node.emit('longclick');
        }
        else
        {
            //console.log('short');
            this.node.emit('shortclick');
        }
        if(this.doubleTimeEclipse<=36)//双击间隔  600ms
        {
            //console.log('double');
            this.node.emit('doubleclick');
        }            
       //开始记录时间
        this.holdTimeEclipse=0;
        this.doubleTimeEclipse=0;
    },this);


//开始监听事件


// called every frame, uncomment this function to activate update callback
 update: function (dt) {
    if(this.holdClick)
    {
        this.holdTimeEclipse++;
        if(this.holdTimeEclipse>120)//如果长按时间大于2s,则认为长按了2s
        {
            this.holdTimeEclipse=120;
        }
    }

    this.doubleTimeEclipse++;
    if(this.doubleTimeEclipse>60)//如果自上次单击,1s后仍没有单击,检测为1s  为了防止溢出
    {
        this.doubleTimeEclipse=60;
    }
 },

});
`

1赞

让我来做一点微小的工作,
Date.Now()可以获取当前的毫秒时间戳

你这个有 bug,要监听 cancel 事件

怎么说? 没有cancel事件啊?

嗯,前端的date对象

使用date的方式的确会好很多