TestMaskPos.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. button: {
  14. default: [],
  15. type: [cc.Node],
  16. serializable: true,
  17. },
  18. stencil: cc.Node,
  19. stencil_tip: cc.Node,
  20. targetButton: {
  21. default: null,
  22. type: cc.Node,
  23. visible:false,
  24. serializable: false,
  25. },
  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. start() {
  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. },
  42. TouchEndFunction(event) {
  43. let pt = this.stencil.convertToNodeSpace(event.getLocation());
  44. let rect = cc.rect(0, 0, this.stencil.width, this.stencil.height);
  45. //点中空洞,返回false,触摸事件继续派发
  46. if (rect.contains(pt)) {
  47. console.log('点击区域');
  48. if (this.targetButton)
  49. cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents,event);
  50. let i = this.random(1, 0);
  51. console.log(i);
  52. let button = this.button[i];
  53. this.targetButton = button.getComponent(cc.Button);
  54. let pt = this.stencil.parent.convertToNodeSpaceAR(button.parent.convertToWorldSpaceAR(button.position));
  55. this.stencil.position = pt;
  56. this.stencil_tip.position = pt;
  57. return false;
  58. }
  59. console.log('点击区域无效');
  60. },
  61. // update (dt) {},
  62. onButtonEvent(target,event) {
  63. console.log(target,event);
  64. }
  65. });