Touch.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.GameMode = cc.find("GameMode").getComponent("GameMode");
  10. this.nodeArr = [];
  11. },
  12. ontouch_start(event) {
  13. // 获取节点坐标
  14. this.touchStartPoint = event.getLocation();
  15. },
  16. //以下部分参考用,具体识别用自己的算法
  17. ontouch_End(event) {
  18. this.touchEndPoint = event.getLocation();
  19. this.calcDir();
  20. },
  21. update(dt) {
  22. this.scheduleOnce(function() {
  23. if (this.GameMode._gamestart) {
  24. this.node.on(cc.Node.EventType.TOUCH_START, this.ontouch_start, this);
  25. this.node.on(cc.Node.EventType.TOUCH_END, this.ontouch_End, this);
  26. }
  27. }, 4);
  28. },
  29. calcDir: function() {
  30. let dirvecX = this.touchEndPoint.x - this.touchStartPoint.x;
  31. let dirvecY = this.touchEndPoint.y - this.touchStartPoint.y;
  32. if (dirvecY >= 0) {
  33. if (dirvecX > 0) {
  34. //向右 上 出左拳
  35. //console.log("右上滑!");
  36. this.node.emit("gesture", { name: 'right_top' });
  37. } else if (dirvecX < 0) {
  38. //向左上 出右拳
  39. //console.log("左上滑!");
  40. this.node.emit("gesture", { name: 'left_top' });
  41. }
  42. } else if (dirvecY <= 0) {
  43. if (dirvecX > 15) {
  44. //console.log("右下滑!");
  45. this.node.emit("gesture", { name: 'right_down' });
  46. } else if (dirvecX < -15) {
  47. // console.log("左下滑!");
  48. this.node.emit("gesture", { name: 'left_down' });
  49. } else if (dirvecX <= 15 && dirvecX >= -15) {
  50. // console.log("向下");
  51. this.node.emit("gesture", { name: 'down' });
  52. }
  53. }
  54. },
  55. });