Touch.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. // nodeArr:[cc.Node]
  5. touchStartPoint: cc.Vec2,
  6. touchEndPoint: cc.Vec2,
  7. },
  8. onLoad() {
  9. this.node.on(cc.Node.EventType.TOUCH_START, this.ontouch_start, this);
  10. this.node.on(cc.Node.EventType.TOUCH_END, this.ontouch_End, this);
  11. this.nodeArr = [];
  12. },
  13. ontouch_start(event) {
  14. // 获取节点坐标
  15. this.touchStartPoint = event.getLocation();
  16. },
  17. //以下部分参考用,具体识别用自己的算法
  18. ontouch_End(event) {
  19. this.touchEndPoint = event.getLocation();
  20. this.calcDir();
  21. },
  22. calcDir: function() {
  23. let dirvecX = this.touchEndPoint.x - this.touchStartPoint.x;
  24. let dirvecY = this.touchEndPoint.y - this.touchStartPoint.y;
  25. if (dirvecY >= 0) {
  26. if (dirvecX > 0) {
  27. //向右 上 出左拳
  28. //console.log("右上滑!");
  29. this.node.emit("gesture", { name: 'right_top' });
  30. } else if (dirvecX < 0) {
  31. //向左上 出右拳
  32. //console.log("左上滑!");
  33. this.node.emit("gesture", { name: 'left_top' });
  34. }
  35. } else if (dirvecY <= 0) {
  36. if (dirvecX > 15) {
  37. //console.log("右下滑!");
  38. this.node.emit("gesture", { name: 'right_down' });
  39. } else if (dirvecX < -15) {
  40. // console.log("左下滑!");
  41. this.node.emit("gesture", { name: 'left_down' });
  42. } else if (dirvecX <= 15 && dirvecX >= -15) {
  43. // console.log("向下");
  44. this.node.emit("gesture", { name: 'down' });
  45. }
  46. }
  47. },
  48. });