MyComponent.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class MyComponent extends cc.Component {
  11. _eventMap = [];
  12. onLoad() {
  13. // super.onLoad();
  14. this.devicesFit();
  15. this.ClickAnyClose()
  16. }
  17. //注册事件
  18. regEvent(type, callback, target = null, once = null) {
  19. cc.systemEvent.on(type, callback, target, once);
  20. this._eventMap.push({
  21. type: type,
  22. callback: callback,
  23. target: target,
  24. once: once
  25. });
  26. }
  27. regEventOnce(type, callback, target = null) {
  28. cc.systemEvent.once(type, callback, target);
  29. this._eventMap.push({
  30. type: type,
  31. callback: callback,
  32. target: target,
  33. once: null
  34. });
  35. }
  36. // 机型适配 子类重载
  37. devicesFit() {
  38. }
  39. onDestroy() {
  40. // super.onDestroy();
  41. for (let i = 0; i < this._eventMap.length; i++) {
  42. let data = this._eventMap[i];
  43. cc.systemEvent.off(data.type, data.callback, data.target);
  44. }
  45. delete this._eventMap;
  46. this.unscheduleAllCallbacks();
  47. }
  48. Pop(_PopType: PopType) {
  49. const url = "Pop/Pop"
  50. cc.resources.load(url, cc.Prefab, (err, Prefab) => {
  51. if (err) {
  52. return
  53. }
  54. let a = cc.instantiate(Prefab);
  55. a.parent = cc.Canvas.instance.node
  56. a.active = true
  57. let Title = a.getChildByName("Title").getComponent(cc.Label)
  58. let Label = a.getChildByName("Label").getComponent(cc.Label)
  59. Title.string = _PopType.Title
  60. Label.string = _PopType.string
  61. let no = a.getChildByName("no button")
  62. let yes = a.getChildByName("yes button")
  63. //添加点击事件
  64. no.on('touchstart', () => {
  65. _PopType.Fail()
  66. a.destroy()
  67. })
  68. //添加点击事件
  69. yes.on('touchstart', () => {
  70. _PopType.OK()
  71. a.destroy()
  72. })
  73. });
  74. }
  75. ClickAnyClose() {
  76. let Mask = this.node.getChildByName("Mask")
  77. Mask?.on('touchstart', () => {
  78. this.node.destroy()
  79. })
  80. }
  81. }