123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- // 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()
- })
- }
- }
|