const {ccclass, property} = cc._decorator;
@ccclass
export default class click extends cc.Component {
@property(cc.Node)
clickBt:cc.Node;
doubleSubTime=200;//ms
longSubTime=600;
startClick=false;
clickTime=0;
startClickTime=0;
endClickTime=0;
onLoad () {
this.clickBt.on(cc.Node.EventType.TOUCH_START,()=>{
this.startClick=true;
this.startClickTime=new Date().getTime();
},this);
this.clickBt.on(cc.Node.EventType.TOUCH_END,()=>{
this.startClick=false;
this.endClickTime=new Date().getTime();
console.log(this.endClickTime-this.startClickTime);
if(this.endClickTime-this.startClickTime>this.longSubTime){
//长按事件
console.log("长按事件");
}else if(this.endClickTime-this.startClickTime<this.doubleSubTime){
//点击事件
this.clickTime++;
setTimeout(()=>{
if(this.clickTime==1){
//单击
console.log("单击事件");
}else if(this.clickTime==2){
//双击
console.log("双击事件");
}
this.clickTime=0;
},this.doubleSubTime)
}
},this);
}
start () {
}
// update (dt) {}
}
cocoscreator 点击、双击、长按事件
最新推荐文章于 2024-02-24 14:32:38 发布
这篇博客介绍了一种JavaScript方法,用于区分节点的点击和长按事件。通过设置不同的时间阈值,当用户触摸开始和结束的时间差超过设定值时,触发长按事件,否则根据时间差判断为单击或双击事件。这种方法可以应用于游戏开发、交互设计等领域,提高用户体验。
摘要由CSDN通过智能技术生成