Touch.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.touch_defance = cc.find("")
  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. update(dt) {
  23. this.scheduleOnce(function() {
  24. if (this.GameMode._gamestart) {
  25. this.node.on(cc.Node.EventType.TOUCH_START, this.ontouch_start, this);
  26. this.node.on(cc.Node.EventType.TOUCH_END, this.ontouch_End, this);
  27. }
  28. }, 4);
  29. },
  30. calcDir: function() {
  31. let dirvecX = this.touchEndPoint.x - this.touchStartPoint.x;
  32. let dirvecY = this.touchEndPoint.y - this.touchStartPoint.y;
  33. if (dirvecY > 0) {
  34. if (dirvecX > 0) {
  35. //向右 上 出左拳
  36. //console.log("右上滑!");
  37. this.node.emit("gesture", { name: 'right_top' });
  38. } else if (dirvecX < 0) {
  39. //向左上 出右拳
  40. //console.log("左上滑!");
  41. this.node.emit("gesture", { name: 'left_top' });
  42. }
  43. } else if (dirvecY < 0) {
  44. if (dirvecX > 0) {
  45. //console.log("右下滑!");
  46. this.node.emit("gesture", { name: 'right_down' });
  47. } else if (dirvecX < 0) {
  48. // console.log("左下滑!");
  49. this.node.emit("gesture", { name: 'left_down' });
  50. }
  51. }
  52. },
  53. btn_touch(event) {
  54. if (event.target.name == "Circle") {
  55. if (this.GameMode._gamestart) {
  56. this.node.emit("gesture", { name: 'down' });
  57. }
  58. }
  59. },
  60. });