GameMode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. let library = require("../Library");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. gameStates: {
  6. default: null,
  7. type: cc.Node,
  8. serializable: true,
  9. },
  10. totalTime: {
  11. default: null,
  12. type: cc.Node,
  13. serializable: true,
  14. },
  15. playButton: {
  16. default: null,
  17. type: cc.Node,
  18. serializable: true,
  19. },
  20. kCal: {
  21. default: null,
  22. type: cc.Node,
  23. serializable: true,
  24. },
  25. resultPlanNode: {
  26. default: null,
  27. type: cc.Node,
  28. serializable: true,
  29. },
  30. },
  31. start () {
  32. //init node
  33. this.gameStatesScpt = this.gameStates.getComponent('GameStates');
  34. this.totalTimeLabel = this.totalTime.getComponent(cc.Label);
  35. this.resultScpt = this.resultPlanNode.getComponent('Result');
  36. this.countingToShowKcal = 0;
  37. },
  38. fireTick()
  39. {
  40. this.schedule(this.tick, 1);
  41. },
  42. tick()
  43. {
  44. //total time
  45. this.gameStatesScpt.currentTime++;
  46. this.totalTimeLabel.string = library.formatSeconds(this.gameStatesScpt.currentTime);
  47. //kcal
  48. this.gameStatesScpt.kCal+=300/3600;
  49. this.countingToShowKcal++;
  50. if(this.countingToShowKcal>10)
  51. {
  52. this.countingToShowKcal = 0;
  53. this.kCal.getComponent(cc.Label).string = this.gameStatesScpt.kCal.toFixed(2).toString();
  54. }
  55. },
  56. untick()
  57. {
  58. this.unschedule(this.tick);
  59. },
  60. //结束
  61. playOver(){
  62. this.resultPlanNode.active = true;
  63. this.resultScpt.onEnd({
  64. hit:this.gameStatesScpt.hitCount,
  65. miss:this.gameStatesScpt.missCount,
  66. time: Math.round((this.gameStatesScpt.currentTime/60)*100)/100,
  67. kCal: Math.round(this.gameStatesScpt.kCal*100)/100
  68. });
  69. },
  70. onPlay()
  71. {
  72. if(this.bPaused)
  73. {
  74. this.playButton.getChildByName('Pause').active = true;
  75. this.playButton.getChildByName('Play').active = false;
  76. this.resume();
  77. }
  78. else
  79. {
  80. this.playButton.getChildByName('Pause').active = false;
  81. this.playButton.getChildByName('Play').active = true;
  82. this.pause();
  83. }
  84. this.bPaused = !this.bPaused;
  85. },
  86. pause()
  87. {
  88. cc.director.pause()
  89. },
  90. resume()
  91. {
  92. cc.director.resume();
  93. },
  94. onReplay()
  95. {
  96. cc.director.loadScene('Game');
  97. }
  98. });