GuideWeak.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * @author 薛鸿潇
  4. */
  5. @ccclass
  6. export default class GuideWeak extends cc.Component {
  7. onLoad() {
  8. gData.guideWeakData.guideStyle = this;
  9. this.InitTouch();
  10. mk.event.register('close-panel', this.onClosePanel, this);
  11. }
  12. update(dt) {
  13. if (!gData.guideWeakData.enable) return;
  14. if (!gData.guideWeakData.data_config.triggerTime) return;
  15. if (gData.guideWeakData.cur_guide_id) return;
  16. if (!mk.guide.isGuideComplete()) return;
  17. if (gData.gameData.gameStyle.node.parent.childrenCount > 1) return;
  18. gData.guideWeakData.silent_time += dt;
  19. if (gData.guideWeakData.silent_time >= gData.guideWeakData.data_config.triggerTime) {
  20. gData.guideWeakData.initGuideId();
  21. gData.guideWeakData.openPanelByBtn();
  22. }
  23. }
  24. /**
  25. * 界面关闭
  26. */
  27. public onClosePanel(panel_name: string = '') {
  28. cc.log(panel_name)
  29. if (gData.guideWeakData.panel_name_config[panel_name] == gData.guideWeakData.cur_guide_id) {
  30. gData.guideWeakData.cur_guide_id = 0;
  31. }
  32. }
  33. /** 玩家的屏幕触摸操作 */
  34. private onClickCall(touch) {
  35. gData.guideWeakData.silent_time = 0;
  36. }
  37. ///////////////// 自定义触摸监听 /////////////////
  38. private _eventManager = cc["internal"]["eventManager"];
  39. private _touchListener: any;
  40. private InitTouch() {
  41. const EventListener = cc["EventListener"];
  42. this._touchListener = EventListener.create({
  43. event: EventListener.TOUCH_ONE_BY_ONE,
  44. swallowTouches: false,//是否吞噬touch事件
  45. owner: this.node,
  46. mask: null,
  47. onTouchBegan: this.onTouchStart.bind(this),
  48. onTouchMoved: null,
  49. onTouchEnded: this.onTouchEnded.bind(this),
  50. onTouchCancelled: null,
  51. });
  52. this._eventManager.addListener(this._touchListener, this.node);
  53. }
  54. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  55. // cc.log("touch start");
  56. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  57. event.bubbles = true;
  58. const point_world_pos = touch.getLocation();
  59. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  60. let result = this.pointInPoly(localPoint, this.node);
  61. return result;
  62. }
  63. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  64. // cc.log("touch end");
  65. this.onClickCall(touch);
  66. }
  67. /**
  68. * 点是否在节点宽高范围内
  69. * @param point 点击的点
  70. * @param node
  71. * @returns
  72. */
  73. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  74. const self_node_pos = node.getPosition();
  75. if (point.x >= (self_node_pos.x - (this.node.width / 2)) &&
  76. point.x <= (self_node_pos.x + (this.node.width / 2)) &&
  77. point.y >= (self_node_pos.y - (this.node.height / 2)) &&
  78. point.y <= (self_node_pos.y + (this.node.height / 2))) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. protected onDestroy(): void {
  84. // super.onDestroy();
  85. this._eventManager.removeListener(this._touchListener, this.node);
  86. mk.event.remove('close-panel', this.onClosePanel, this);
  87. }
  88. }