`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;
}
},
});
`