Touch.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. // nodeArr:[cc.Node]
  5. },
  6. onLoad () {
  7. this.firstX = null;
  8. this.firsty = null;
  9. this.node.on(cc.Node.EventType.TOUCH_START,this.touchBegin,this);
  10. this.node.on(cc.Node.EventType.TOUCH_END,this.touchEnd,this);
  11. this.nodeArr = [];
  12. },
  13. touchBegin:function(event)
  14. {
  15. let location = event.getLocation();// 获取节点坐标
  16. this.firstX = location.x;
  17. this.firstY = location.y;
  18. },
  19. //以下部分参考用,具体识别用自己的算法
  20. touchEnd:function(event)
  21. {
  22. let touchPoint = event.getLocation();
  23. let endX = this.firstX - touchPoint.x;
  24. let endY = this.firstY - touchPoint.y;
  25. if (Math.abs(endX) > Math.abs(endY)){
  26. //手势向左右
  27. //判断向左还是向右
  28. if (endX > 0){
  29. //向左函数
  30. // console.log('left');
  31. this.node.emit("gesture",{name:'left'});
  32. } else {
  33. //向右函数
  34. // console.log('right');
  35. this.node.emit("gesture",{name:'right'});
  36. }
  37. } else {
  38. //手势向上下
  39. //判断手势向上还是向下
  40. if (endY > 0){
  41. //向下函数
  42. // console.log('down');
  43. this.node.emit("gesture",{name:'down'});
  44. } else {
  45. //向上函数
  46. // console.log('up');
  47. this.node.emit("gesture",{name:'up'});
  48. }
  49. }
  50. }
  51. });