TouchEvent.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. player1: {
  5. default: null,
  6. type: cc.Node,
  7. serializable: true,
  8. },
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this.init();
  13. this.registerEvent();
  14. },
  15. init()
  16. {
  17. this.firstX = null;
  18. this.firsty = null;
  19. //playerControllerScript
  20. this.pConSt1 = this.player1.getComponent('BasePlayerController');
  21. this.pStatesSt = this.player1.getChildByName('PlayerStates').getComponent('BasePlayerStates');
  22. },
  23. registerEvent() {
  24. //touchstart 可以换成cc.Node.EventType.TOUCH_START
  25. this.node.on('touchstart', this.onEventStart, this);
  26. //touchmove 可以换成cc.Node.EventType.TOUCH_MOVE
  27. this.node.on('touchmove', this.onEventMove, this);
  28. //touchcancel 可以换成cc.Node.EventType.TOUCH_CANCEL
  29. this.node.on('touchcancel', this.onEventCancel, this);
  30. //touchend 可以换成cc.Node.EventType.TOUCH_END
  31. this.node.on('touchend', this.onEventEnd, this);
  32. },
  33. // start () {},
  34. /**
  35. * 触摸开始
  36. * @param {*} event
  37. */
  38. onEventStart(event) {
  39. if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
  40. //世界坐标
  41. let worldPoint = event.getLocation();
  42. // console.log('start Event \n worldPoint=', worldPoint);
  43. // console.log('touch start');
  44. this.firstX = worldPoint.x;
  45. this.firstY = worldPoint.y;
  46. },
  47. /**
  48. * 触摸移动
  49. * @param {*} event
  50. */
  51. onEventMove(event) {
  52. if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
  53. //世界坐标
  54. let worldPoint = event.getLocation();
  55. // console.log('move Move \n worldPoint=', worldPoint);
  56. },
  57. /**
  58. * 触摸
  59. * 当手指在目标节点区域外离开屏幕时
  60. * 比如说,触摸node的size是200x200。
  61. * 当超过这个区域时,就是触发这个事件
  62. * @param {*} event
  63. */
  64. onEventCancel(event) {
  65. if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
  66. //世界坐标
  67. let worldPoint = event.getLocation();
  68. // console.log('cancel Event \n worldPoint=', worldPoint);
  69. },
  70. /**
  71. * 当手指在目标节点区域内离开屏幕时
  72. * @param {*} event
  73. */
  74. onEventEnd(event) {
  75. if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
  76. //世界坐标
  77. let worldPoint = event.getLocation();
  78. // console.log('end Event \n worldPoint=', worldPoint);
  79. let endX = this.firstX - worldPoint.x;
  80. let endY = this.firstY - worldPoint.y;
  81. // var tempPlayer = node.parent.convertToNodeSpaceAR(touchPoint);
  82. // node.setPosition(tempPlayer);
  83. if (Math.abs(endX) > Math.abs(endY)){
  84. //手势向左右
  85. //判断向左还是向右
  86. if (endX > 0){
  87. //向左函数
  88. // console.log('left');
  89. } else {
  90. //向右函数
  91. // console.log('right');
  92. if (!globalConfig.bAi) {
  93. this.pConSt1.speedUp_lockStep();
  94. }
  95. else{
  96. this.pConSt1.speedUp();
  97. }
  98. }
  99. } else {
  100. //手势向上下
  101. //判断手势向上还是向下
  102. if (endY > 0){
  103. //向下函数
  104. // console.log('down');
  105. } else {
  106. //向上函数
  107. // console.log('up');
  108. if (!globalConfig.bAi) {
  109. this.pConSt1.jump_lockStep();
  110. }
  111. else{
  112. this.pConSt1.jump();
  113. }
  114. }
  115. }
  116. },
  117. // update (dt) {},
  118. });