TaskMask.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. targetButton: {
  5. default: null,
  6. type: cc.Button,
  7. visible: false,
  8. serializable: false,
  9. },
  10. stencil: cc.Node,
  11. stencil_tip: cc.Node,
  12. stencil_touch: cc.Node,
  13. //转换坐标的偏移量
  14. offset: {
  15. default: null,
  16. visible: false,
  17. serializable: false,
  18. },
  19. //需要偏移第几个节点
  20. offsetCount: {
  21. default: -1,
  22. type: cc.Integer,
  23. visible: false,
  24. serializable: false,
  25. },
  26. collider: cc.PolygonCollider
  27. },
  28. random(max, min) {
  29. return Math.floor(Math.random() * (max - min + 1) + min);
  30. },
  31. onLoad() {
  32. // this.node.on(cc.Node.EventType.TOUCH_START, this.TouchStartFunction, this);
  33. // this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEndFunction, this);
  34. // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.TouchCancelFunction, this);
  35. //箭头动画
  36. // this.stencil_tip.opacity = 255;
  37. this.stencil_tip.stopAllActions();
  38. var s = cc.sequence(cc.scaleTo(0.5, 1.5), cc.scaleTo(0.5, 1.0));
  39. var repeat = cc.repeatForever(s);
  40. this.stencil_tip.runAction(repeat);
  41. // console.log("TOUCH_START",this.collider);
  42. this.stencil_touch.on(cc.Node.EventType.TOUCH_START, function (touch, event) {
  43. // console.log("Hit!");
  44. // // 返回世界坐标
  45. // let touchLoc = touch.getLocation();
  46. // console.log('this.collider.world.points=', this.collider.world.points);
  47. // // // https://docs.cocos.com/creator/api/zh/classes/Intersection.html 检测辅助类
  48. // if (cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
  49. // console.log("Hit!");
  50. // }
  51. // else {
  52. // console.log("No hit");
  53. // }
  54. if (this.targetButton) {
  55. cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
  56. this.count++;
  57. if (this.count < this.targetNodes.length) {
  58. this._setMaskPosAndEvent();
  59. } else {
  60. GlobalD.GameControl.isTaskInProgress = false;
  61. this.node.destroy();
  62. }
  63. }
  64. }, this);
  65. },
  66. /**
  67. * 初始设置目标参数
  68. * @method onInitTaskMask
  69. * @param {[Node]} _targetNodes 透明区域指向的位置节点对象数组
  70. * @param {Button} _targetButton 需要传递事件的按钮对象
  71. */
  72. onInitTaskMask(_targetNodes) {
  73. GlobalD.GameControl.isTaskInProgress = true;
  74. // this.nodeLength = _targetNodes.length;
  75. this.targetNodes = _targetNodes;
  76. this.count = 0;
  77. this._setMaskPosAndEvent();
  78. },
  79. //设置提示位置和绑定对应的按钮事件
  80. _setMaskPosAndEvent() {
  81. let pt = this.stencil.parent.convertToNodeSpaceAR(this.targetNodes[this.count].parent.convertToWorldSpaceAR(this.targetNodes[this.count].position));
  82. //设置最后一个点的偏移量
  83. //铺路时候,场景的位置不准确需要修正
  84. // console.log(this.offset, this.offsetCount);
  85. if (this.offset && this.offsetCount == this.count)
  86. pt = cc.v2(pt.x + this.offset.x, pt.y + this.offset.y);
  87. this.stencil.position = pt;
  88. let _size = this.targetNodes[this.count].width > this.targetNodes[this.count].height ? this.targetNodes[this.count].width : this.targetNodes[this.count].height;
  89. this.stencil.width = _size + 5;
  90. this.stencil.height = _size + 5;
  91. this.stencil_tip.position = cc.v2(pt.x, pt.y + _size * 0.4);
  92. this.stencil_touch.position = pt;
  93. this.stencil_touch.width = _size + 2;
  94. this.stencil_touch.height = _size + 2;
  95. let _targetButton = this.targetNodes[this.count].getComponent(cc.Button);
  96. if (_targetButton)
  97. this.targetButton = _targetButton;
  98. else
  99. console.error('遮挡模板的按钮对象不存在;');
  100. },
  101. isInCircle: function (pos, cPos, r) {
  102. console.log('pospos:', pos, cPos, r, pos.sub(cPos).mag());
  103. if (pos.sub(cPos).mag() <= r) {
  104. return true;
  105. }
  106. return false;
  107. },
  108. TouchEndFunction(event) {
  109. console.log('点击区域1');
  110. let pt = this.stencil.parent.convertToNodeSpaceAR(event.getLocation());
  111. let x = this.stencil.position.x;
  112. let y = this.stencil.position.y;
  113. // let rect = cc.rect(0, 0, this.stencil.width, this.stencil.height);
  114. //点中空洞,返回false,触摸事件继续派发
  115. // if (rect.contains(pt)) {
  116. if (this.isInCircle(pt, cc.v2(x, y), this.stencil.width)) {
  117. console.log('点击区域');
  118. return;
  119. if (this.targetButton) {
  120. cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
  121. this.count++;
  122. if (this.count < this.targetNodes.length) {
  123. this._setMaskPosAndEvent();
  124. } else {
  125. GlobalD.GameControl.isTaskInProgress = false;
  126. this.node.destroy();
  127. }
  128. }
  129. return false;
  130. }
  131. // console.log('点击区域无效:', pt);
  132. },
  133. // update (dt) {},
  134. onButtonEvent(target, event) {
  135. console.log(target, event);
  136. }
  137. });