Menu.ts 4.8 KB

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