cocoscreator 点击、双击、长按事件

这篇博客介绍了一种JavaScript方法,用于区分节点的点击和长按事件。通过设置不同的时间阈值,当用户触摸开始和结束的时间差超过设定值时,触发长按事件,否则根据时间差判断为单击或双击事件。这种方法可以应用于游戏开发、交互设计等领域,提高用户体验。
摘要由CSDN通过智能技术生成

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) {}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值