SettingsPanel.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { _decorator, Button, Component, director, Node, tween, UIOpacity } from 'cc';
  2. import { UIManager } from './UIManager';
  3. import ch_audio from '../../ch/audio/audio';
  4. import { BasePanel } from './BasePanel';
  5. import { MyGame } from '../Game/MyGame';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('SettingsPanel')
  8. export class SettingsPanel extends BasePanel {
  9. private musicBtn:Button=null;
  10. private effectBtn:Button=null;
  11. private VibrateBtn:Button=null;
  12. private returnHallBtn:Button=null;
  13. private switchMusic:boolean=true;
  14. private switchEffect:boolean=true;
  15. private mask_1:Node=null;
  16. private mask_2:Node=null;
  17. private mask_3:Node=null;
  18. start() {
  19. this.musicBtn=this.node.getChildByPath('MusicVolume/Button').getComponent(Button);
  20. this.effectBtn=this.node.getChildByPath('EffectVolume/Button').getComponent(Button);
  21. this.VibrateBtn=this.node.getChildByPath('Vibrate/Button').getComponent(Button);
  22. this.returnHallBtn=this.node.getChildByName('ReturnHallBtn').getComponent(Button);
  23. //初始化用户上次的设置
  24. this.musicBtn.node.on(Button.EventType.CLICK, this.onSwitchMusic, this);
  25. this.effectBtn.node.on(Button.EventType.CLICK, this.onSwitchEffect, this);
  26. this.VibrateBtn.node.on(Button.EventType.CLICK, this.onSwitchVibrate, this);
  27. this.returnHallBtn.node.on(Button.EventType.CLICK, this.ReturnHall, this);
  28. this.mask_1=this.node.getChildByPath('MusicVolume/Mask');
  29. this.mask_2=this.node.getChildByPath('EffectVolume/Mask');
  30. this.mask_3=this.node.getChildByPath('Vibrate/Mask');
  31. this.mask_1.active=false;
  32. this.mask_2.active=false;
  33. }
  34. update(deltaTime: number) {
  35. }
  36. //音乐开关
  37. public onSwitchMusic(){
  38. ch_audio.getInstance().switchMusic = !ch_audio.getInstance().switchMusic;
  39. if( ch_audio.getInstance().switchMusic)
  40. {
  41. console.log('play music');
  42. //隐藏遮罩
  43. this.mask_1.active=false;
  44. ch_audio.getInstance().volumeMusic=1;
  45. }
  46. else
  47. {
  48. console.log('stop music');
  49. //显示遮罩
  50. this.mask_1.active=true;
  51. ch_audio.getInstance().volumeMusic=0;
  52. }
  53. }
  54. //音效开关
  55. public onSwitchEffect(){
  56. ch_audio.getInstance().switchEffect = !ch_audio.getInstance().switchEffect;
  57. if(ch_audio.getInstance().switchEffect)
  58. {
  59. console.log('play effect');
  60. //隐藏遮罩
  61. this.mask_2.active=false;
  62. ch_audio.getInstance().volumeEffect=1;
  63. }
  64. else
  65. {
  66. console.log('stop effect');
  67. //显示遮罩
  68. this.mask_2.active=true;
  69. ch_audio.getInstance().volumeEffect=0;
  70. }
  71. }
  72. public onSwitchVibrate(){
  73. //震动开关
  74. }
  75. ReturnHall(){
  76. MyGame.getInstance().setLastScene("Main");
  77. //返回大厅
  78. ch_audio.getInstance().stop();
  79. UIManager.Instance.clearStack();
  80. const sceneName = 'Hall';
  81. director.loadScene(sceneName, (err, scene) => {
  82. if (err) {
  83. console.error('场景加载失败:', err);
  84. return;
  85. }
  86. });
  87. }
  88. onClose(){
  89. super.onClose(null);
  90. }
  91. }