Menu.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { _decorator, Button, Component,director,game,Node,Sprite, SpriteFrame } from 'cc';
  2. import { GameData } from './GameData';
  3. import { MainScene } from './MainScene';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('Menu')
  6. export class Menu extends Component {
  7. //加速Buton
  8. @property(Button)
  9. btnSpeed:Button = null;
  10. @property(SpriteFrame)
  11. imgSpeed_1:SpriteFrame = null;
  12. @property(SpriteFrame)
  13. imgSpeed_2:SpriteFrame = null;
  14. //暂停Button
  15. @property(Button)
  16. btnPause:Button = null;
  17. @property(SpriteFrame)
  18. imgPause_1:SpriteFrame = null;
  19. @property(SpriteFrame)
  20. imgPause_2:SpriteFrame = null;
  21. @property(SpriteFrame)
  22. imgMenuCenter_1:SpriteFrame = null;//第几波怪物
  23. @property(SpriteFrame)
  24. imgMenuCenter_2:SpriteFrame = null;//暂停中
  25. //弹出框
  26. //@property(Node)
  27. dialog:Node = null;
  28. isAccelerate:boolean = false;//是否加速
  29. currentState:boolean = null;//当前状态
  30. isPause:boolean = false;//是否暂停
  31. currentPauseState:boolean = null;//当前状态
  32. currentSpeed:number = null;//当前速度
  33. start(){
  34. this.dialog = this.node.getChildByName("Dialog");
  35. this.dialog.active = false;
  36. this.currentSpeed = GameData.speed;//将初始速度赋值给当前速度
  37. // let btnQuit = this.dialog.getChildByName("BtnQuit");
  38. // btnQuit.on(Button.EventType.CLICK,this.onBtnQuitDialog,this)
  39. // let btnSelect = this.dialog.getChildByName("BtnSelect");
  40. // btnSelect.on(Button.EventType.CLICK,this.onBtnSelectDialog,this);
  41. // let btnRestart = this.dialog.getChildByName("BtnRestart");
  42. // btnRestart.on(Button.EventType.CLICK,this.onBtnRestartDialog,this);
  43. // let btnResume = this.dialog.getChildByName("BtnResume");
  44. // btnResume.on(Button.EventType.CLICK,this.onBtnQuitDialog,this);
  45. const buttonConfigs = [
  46. {name : "BtnResume",handler : this.onBtnQuitDialog},
  47. {name : "BtnRestart",handler : this.onBtnRestartDialog},
  48. {name : "BtnSelect",handler : this.onBtnSelectDialog},
  49. {name : "BtnQuit",handler : this.onBtnQuitDialog}
  50. ]
  51. buttonConfigs.forEach(config =>{
  52. let button = this.dialog.getChildByName(config.name);
  53. button.on(Button.EventType.CLICK,config.handler,this);
  54. });
  55. this.currentState = this.isAccelerate; //当前状态没有加速
  56. this.btnSpeed.getComponent(Sprite).spriteFrame = this.imgSpeed_1;
  57. this.currentPauseState = this.isPause;
  58. this.btnPause.getComponent(Sprite).spriteFrame = this.imgPause_1;
  59. this.node.getChildByName("MenuCenter_01_CN").getComponent(Sprite).spriteFrame = this.imgMenuCenter_1;
  60. }
  61. //加速
  62. onBtnSpeed(){
  63. let btnSpeedComponent = this.btnSpeed.getComponent(Sprite);
  64. //获取父节点MainScene的脚本组件
  65. let mainSceneTS = this.node.parent.getComponent(MainScene);
  66. this.isAccelerate = !this.isAccelerate;//加速
  67. this.currentState = this.isAccelerate;//当前状态为加速
  68. if(!this.isAccelerate){
  69. //一倍速
  70. mainSceneTS.speed = this.currentSpeed;
  71. btnSpeedComponent.spriteFrame = this.imgSpeed_1;
  72. }else{
  73. //二倍速
  74. mainSceneTS.speed = mainSceneTS.speed * 2
  75. btnSpeedComponent.spriteFrame = this.imgSpeed_2;
  76. }
  77. }
  78. //暂停游戏
  79. onBtnPause(){
  80. let menuCenterComponent = this.node.getChildByName("MenuCenter_01_CN").getComponent(Sprite);
  81. let btnPauseComponent = this.btnPause.getComponent(Sprite);
  82. this.isPause = !this.isPause;//暂停
  83. this.currentPauseState = this.isPause;//当前状态为暂停
  84. if(!this.isPause){
  85. menuCenterComponent.spriteFrame = this.imgMenuCenter_1;
  86. btnPauseComponent.spriteFrame = this.imgPause_1;
  87. //恢复暂停
  88. if(director.isPaused){
  89. director.resume();
  90. return;
  91. }
  92. }else{
  93. //暂停
  94. director.pause();
  95. menuCenterComponent.spriteFrame = this.imgMenuCenter_2;
  96. btnPauseComponent.spriteFrame = this.imgPause_2;
  97. }
  98. }
  99. //弹出dialog
  100. onBtnMore(){
  101. console.log("More")
  102. director.pause();
  103. this.dialog.active = true;
  104. }
  105. //退出 恢复暂停
  106. onBtnQuitDialog(){
  107. console.log("quitDialog");
  108. if(director.isPaused()){
  109. director.resume();
  110. return;
  111. }
  112. this.dialog.active = false;
  113. }
  114. //重新选择关卡
  115. onBtnSelectDialog(){
  116. director.loadScene("SelectLevel");
  117. console.log("SelectLevel");
  118. }
  119. //重新开始游戏
  120. onBtnRestartDialog(){
  121. console.log("Restart");
  122. }
  123. update(deltaTime: number) {
  124. }
  125. }