MyComponent.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import { PopType } from "../EventName/EventName";
  8. /**
  9. 按钮点击限制节流 防抖 节流
  10. @param lockTime 阻塞时间
  11. @param callBackFun 节流回调 多次点击的时候给一个回调函数提示用户不要多次点击
  12. */
  13. export function ButtonLock(lockTime: number = 0.3, callBackFun?: Function) {
  14. return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  15. let oldFun: Function = descriptor.value;
  16. let isLock: boolean = false;
  17. descriptor.value = function (...args: any[]) {
  18. if (isLock) {
  19. callBackFun?.()
  20. return
  21. }
  22. isLock = true;
  23. setTimeout(() => {
  24. isLock = false
  25. }, lockTime * 1000);
  26. oldFun.apply(this, args);
  27. }
  28. return descriptor
  29. }
  30. }
  31. const { ccclass, property } = cc._decorator;
  32. @ccclass
  33. export default class MyComponent extends cc.Component {
  34. _eventMap = [];
  35. onLoad() {
  36. // super.onLoad();
  37. this.devicesFit();
  38. this.ClickAnyClose()
  39. }
  40. //注册事件
  41. regEvent(type, callback, target = null, once = null) {
  42. cc.systemEvent.on(type, callback, target, once);
  43. this._eventMap.push({
  44. type: type,
  45. callback: callback,
  46. target: target,
  47. once: once
  48. });
  49. }
  50. regEventOnce(type, callback, target = null) {
  51. cc.systemEvent.once(type, callback, target);
  52. this._eventMap.push({
  53. type: type,
  54. callback: callback,
  55. target: target,
  56. once: null
  57. });
  58. }
  59. // 机型适配 子类重载
  60. devicesFit() {
  61. }
  62. onDestroy() {
  63. // super.onDestroy();
  64. for (let i = 0; i < this._eventMap.length; i++) {
  65. let data = this._eventMap[i];
  66. cc.systemEvent.off(data.type, data.callback, data.target);
  67. }
  68. delete this._eventMap;
  69. this.unscheduleAllCallbacks();
  70. }
  71. Pop(_PopType: PopType) {
  72. const url = "res/Pop/Pop"
  73. // cc.resources.load(url, cc.Prefab, (err, Prefab) => {
  74. // if (err) {
  75. // return
  76. // }
  77. // let a = cc.instantiate(Prefab);
  78. // a.parent = cc.Canvas.instance.node
  79. // a.active = true
  80. // let Title = a.getChildByName("Title").getComponent(cc.Label)
  81. // let Label = a.getChildByName("Label").getComponent(cc.Label)
  82. // Title.string = _PopType.Title
  83. // Label.string = _PopType.string
  84. // let no = a.getChildByName("no button")
  85. // let yes = a.getChildByName("yes button")
  86. // //添加点击事件
  87. // no.on('touchstart', () => {
  88. // _PopType.Fail()
  89. // a.destroy()
  90. // })
  91. // //添加点击事件
  92. // yes.on('touchstart', () => {
  93. // _PopType.OK()
  94. // a.destroy()
  95. // })
  96. // });
  97. let bundle = cc.assetManager.getBundle("sub");
  98. bundle.load(url, cc.Prefab, (err: Error, Prefab) => {
  99. if (err) {
  100. return
  101. }
  102. let a = cc.instantiate(Prefab);
  103. a.parent = cc.Canvas.instance.node
  104. a.active = true
  105. let Title = a.getChildByName("Title").getComponent(cc.Label)
  106. let Label = a.getChildByName("Label").getComponent(cc.Label)
  107. Title.string = _PopType.Title
  108. Label.string = _PopType.string
  109. let no = a.getChildByName("no button")
  110. let yes = a.getChildByName("yes button")
  111. //添加点击事件
  112. no.on('touchstart', () => {
  113. _PopType.Fail()
  114. a.destroy()
  115. })
  116. //添加点击事件
  117. yes.on('touchstart', () => {
  118. _PopType.OK()
  119. a.destroy()
  120. })
  121. });
  122. }
  123. ClickAnyClose() {
  124. let Mask = this.node.getChildByName("Mask")
  125. Mask?.on('touchstart', () => {
  126. this.node.destroy()
  127. })
  128. }
  129. }