TouchEvent.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. onLoad () {
  6. this.init();
  7. this.registerEvent();
  8. },
  9. init()
  10. {
  11. this.firstX = null;
  12. this.firsty = null;
  13. this.listenerArr = [];
  14. },
  15. registerEvent() {
  16. //touchstart 可以换成cc.Node.EventType.TOUCH_START
  17. this.node.on('touchstart', this.onEventStart, this);
  18. //touchmove 可以换成cc.Node.EventType.TOUCH_MOVE
  19. this.node.on('touchmove', this.onEventMove, this);
  20. //touchcancel 可以换成cc.Node.EventType.TOUCH_CANCEL
  21. this.node.on('touchcancel', this.onEventCancel, this);
  22. //touchend 可以换成cc.Node.EventType.TOUCH_END
  23. this.node.on('touchend', this.onEventEnd, this);
  24. },
  25. registerListener(listenerNode)
  26. {
  27. // 要判断对象是否已经注册过,如果存在不注册
  28. if(this.indexOf(this.listenerArr, listenerNode) != -1) return;
  29. this.listenerArr.push(listenerNode);
  30. },
  31. unRegisterListener(listenerNode)
  32. {
  33. // 要判断对象是否已经注册过,如果存在才可以删除
  34. if(this.indexOf(this.listenerArr, listenerNode) != -1)
  35. {
  36. this.remove(this.listenerArr, listenerNode);
  37. }
  38. },
  39. dispatchEvent(eventName, data)//把所有的监听事件分发给所有接收到回调的节点
  40. {
  41. for (let i = 0; i < this.listenerArr.length; i++) {
  42. this.listenerArr[i].emit(eventName, data);
  43. }
  44. },
  45. indexOf(arr, item) {//判断元素在数组第几位
  46. for (let i = 0; i < arr.length; i++) {
  47. if (arr[i] == item) return i;
  48. }
  49. return -1;
  50. },
  51. /**
  52. * 触摸开始
  53. * @param {*} event
  54. */
  55. onEventStart(event) {
  56. //世界坐标
  57. let worldPoint = event.getLocation();
  58. // console.log('start Event \n worldPoint=', worldPoint);
  59. // console.log('touch start');
  60. this.firstX = worldPoint.x;
  61. this.firstY = worldPoint.y;
  62. this.dispatchEvent('touchstart', worldPoint);
  63. },
  64. /**
  65. * 触摸移动
  66. * @param {*} event
  67. */
  68. onEventMove(event) {
  69. //世界坐标
  70. let worldPoint = event.getLocation();
  71. // console.log('move Move \n worldPoint=', worldPoint);
  72. },
  73. /**
  74. * 触摸
  75. * 当手指在目标节点区域外离开屏幕时
  76. * 比如说,触摸node的size是200x200。
  77. * 当超过这个区域时,就是触发这个事件
  78. * @param {*} event
  79. */
  80. onEventCancel(event) {
  81. //世界坐标
  82. let worldPoint = event.getLocation();
  83. // console.log('cancel Event \n worldPoint=', worldPoint);
  84. },
  85. /**
  86. * 当手指在目标节点区域内离开屏幕时
  87. * @param {*} event
  88. */
  89. onEventEnd(event) {
  90. //世界坐标
  91. let worldPoint = event.getLocation();
  92. // console.log('end Event \n worldPoint=', worldPoint);
  93. let endX = this.firstX - worldPoint.x;
  94. let endY = this.firstY - worldPoint.y;
  95. // var tempPlayer = node.parent.convertToNodeSpaceAR(touchPoint);
  96. // node.setPosition(tempPlayer);
  97. if (Math.abs(endX) > Math.abs(endY)){
  98. //手势向左右
  99. //判断向左还是向右
  100. if (endX > 0){
  101. //向左函数
  102. // console.log('left');
  103. this.dispatchEvent('swipe', 'left');
  104. } else {
  105. //向右函数
  106. // console.log('right');
  107. this.dispatchEvent('swipe', 'right');
  108. }
  109. } else {
  110. //手势向上下
  111. //判断手势向上还是向下
  112. if (endY > 0){
  113. //向下函数
  114. // console.log('down');
  115. this.dispatchEvent('swipe', 'down');
  116. } else {
  117. //向上函数
  118. // console.log('up');
  119. this.dispatchEvent('swipe', 'up');
  120. }
  121. }
  122. },
  123. });