PlayerController.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. GameMode: {
  5. default: null,
  6. type: cc.Node,
  7. serializable: true,
  8. },
  9. GameStates: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. PlayerNode: {
  15. default: null,
  16. type: cc.Node,
  17. serializable: true,
  18. },
  19. CharactorNode: {
  20. default: null,
  21. type: cc.Node,
  22. serializable: true,
  23. },
  24. PuchTimesLabel: {
  25. default: null,
  26. type: cc.Label,
  27. serializable: true,
  28. },
  29. },
  30. start () {
  31. this.GameModeScp = this.GameMode.getComponent('GameMode');
  32. this.GameStatesScp = this.GameStates.getComponent('GameStates');
  33. this.CharactorScp = this.CharactorNode.getComponent('Charactor');
  34. this.TouchEventScp = cc.find('Canvas').getComponent('TouchEvent');
  35. this.TouchEventScp.registerListener(this.node);
  36. this.node.on('touchstart',this.onEventStart,this);
  37. this.node.on('swipe',this.onEventSwipe,this);
  38. this.GameStatesScp.startPosition.x = this.PlayerNode.x;
  39. this.GameStatesScp.startPosition.y = this.PlayerNode.y;
  40. this.bRuning = false;
  41. },
  42. onEventStart(worldPoint)
  43. {
  44. if(!this.GameStatesScp.bStart) return;
  45. console.log('touch start');
  46. this.Run();
  47. },
  48. onEventSwipe(direction)
  49. {
  50. if(!this.GameStatesScp.bStart) return;
  51. if(direction == 'left')
  52. {
  53. //console.log('left');
  54. }
  55. else if(direction == 'right')
  56. {
  57. //console.log('right');
  58. }
  59. else if(direction == 'up')
  60. {
  61. //console.log('up');
  62. }
  63. else if(direction == 'down')
  64. {
  65. //console.log('down');
  66. }
  67. },
  68. Run()
  69. {
  70. this.CharactorScp.Run();
  71. this.PlayerNode.x+=3;
  72. this.GameStatesScp.PunchTimes++;
  73. this.PuchTimesLabel.string = this.GameStatesScp.PunchTimes;
  74. this.unschedule(this.StopRun);
  75. this.scheduleOnce(this.StopRun,0.5);
  76. },
  77. StopRun()
  78. {
  79. let count = 2;
  80. this.schedule(function(){
  81. if(!this.GameStatesScp.bStart)
  82. {
  83. this.CharactorScp.Idel();
  84. return;
  85. }
  86. this.PlayerNode.x+=1;
  87. count--;
  88. if(count==0)
  89. {
  90. this.CharactorScp.Idel();
  91. }
  92. }.bind(this),0.1,count)
  93. },
  94. Reset()
  95. {
  96. this.PlayerNode.x = this.GameStatesScp.startPosition.x;
  97. this.PlayerNode.y = this.GameStatesScp.startPosition.y;
  98. this.PuchTimesLabel.string = '0';
  99. }
  100. });