MyComponent.ts 2.4 KB

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