ts 防止按钮连点(自定义脚本) cocos creator

思路是通过 clickEvents 注册的点击事件置空 , 暂时阻止button连点 , 0.5 秒的安全时间过去之后 , 在把 clickEvents 打开即可 .

const {ccclass, property} = cc._decorator;

@ccclass
export default class ButtonBase extends cc.Component {

    private safeTime = 0.5;
    private clickEvents = [];

    onLoad () {

        let button = this.getComponent(cc.Button);
        if (!button){
            return;
        }

        this.clickEvents = button.clickEvents;

        this.node.on('click', ()=>{
            if (button.clickEvents.length == 0) return;
            button.clickEvents = [];
            this.scheduleOnce((dt)=>{
                if (this && this.node && button)
                    button.clickEvents = this.clickEvents;
            }, this.safeTime);
        }, this);
    }

}

你可能感兴趣的:(ts 防止按钮连点(自定义脚本) cocos creator)