// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import { PopType } from "../EventName/EventName"; /** 按钮点击限制节流 防抖 节流 @param lockTime 阻塞时间 @param callBackFun 节流回调 多次点击的时候给一个回调函数提示用户不要多次点击 */ export function ButtonLock(lockTime: number = 0.3, callBackFun?: Function) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { let oldFun: Function = descriptor.value; let isLock: boolean = false; descriptor.value = function (...args: any[]) { if (isLock) { callBackFun?.() return } isLock = true; setTimeout(() => { isLock = false }, lockTime * 1000); oldFun.apply(this, args); } return descriptor } } const { ccclass, property } = cc._decorator; @ccclass export default class MyComponent extends cc.Component { _eventMap = []; onLoad() { // super.onLoad(); this.devicesFit(); this.ClickAnyClose() } //注册事件 regEvent(type, callback, target = null, once = null) { cc.systemEvent.on(type, callback, target, once); this._eventMap.push({ type: type, callback: callback, target: target, once: once }); } regEventOnce(type, callback, target = null) { cc.systemEvent.once(type, callback, target); this._eventMap.push({ type: type, callback: callback, target: target, once: null }); } // 机型适配 子类重载 devicesFit() { } onDestroy() { // super.onDestroy(); for (let i = 0; i < this._eventMap.length; i++) { let data = this._eventMap[i]; cc.systemEvent.off(data.type, data.callback, data.target); } delete this._eventMap; this.unscheduleAllCallbacks(); } Pop(_PopType: PopType) { const url = "res/Pop/Pop" // cc.resources.load(url, cc.Prefab, (err, Prefab) => { // if (err) { // return // } // let a = cc.instantiate(Prefab); // a.parent = cc.Canvas.instance.node // a.active = true // let Title = a.getChildByName("Title").getComponent(cc.Label) // let Label = a.getChildByName("Label").getComponent(cc.Label) // Title.string = _PopType.Title // Label.string = _PopType.string // let no = a.getChildByName("no button") // let yes = a.getChildByName("yes button") // //添加点击事件 // no.on('touchstart', () => { // _PopType.Fail() // a.destroy() // }) // //添加点击事件 // yes.on('touchstart', () => { // _PopType.OK() // a.destroy() // }) // }); let bundle = cc.assetManager.getBundle("sub"); bundle.load(url, cc.Prefab, (err: Error, Prefab) => { if (err) { return } let a = cc.instantiate(Prefab); a.parent = cc.Canvas.instance.node a.active = true let Title = a.getChildByName("Title").getComponent(cc.Label) let Label = a.getChildByName("Label").getComponent(cc.Label) Title.string = _PopType.Title Label.string = _PopType.string let no = a.getChildByName("no button") let yes = a.getChildByName("yes button") //添加点击事件 no.on('touchstart', () => { _PopType.Fail() a.destroy() }) //添加点击事件 yes.on('touchstart', () => { _PopType.OK() a.destroy() }) }); } ClickAnyClose() { let Mask = this.node.getChildByName("Mask") Mask?.on('touchstart', () => { this.node.destroy() }) } }