TaskMask.js 5.2 KB

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