TouchEvent.js 4.3 KB

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