MusicBar.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { _decorator, ccenum, Component, Enum, Node, Sprite } from 'cc';
  2. import { audioManager } from './AudioManager';
  3. const { ccclass, property } = _decorator;
  4. enum MusicType{
  5. music,
  6. sound,
  7. vibrate,
  8. }
  9. @ccclass('MusicBar')
  10. export class MusicBar extends Component {
  11. @property({ type: Enum(MusicType) })
  12. readonly musicType:MusicType
  13. @property(Node)
  14. openBtn:Node
  15. @property(Node)
  16. closeBtn:Node
  17. protected onEnable(): void {
  18. this.openBtn?.on(Node.EventType.TOUCH_END,this.clickOpen,this)
  19. this.closeBtn?.on(Node.EventType.TOUCH_END,this.clickClose,this)
  20. if(this.musicType ==MusicType.music ){
  21. this.flash(audioManager.switchMusic)
  22. }
  23. if(this.musicType ==MusicType.sound ){
  24. this.flash(audioManager.switchEffect)
  25. }
  26. if(this.musicType ==MusicType.vibrate ){
  27. this.flash(audioManager.switch_vibrate)
  28. }
  29. }
  30. clickSwich(boolean){
  31. // audioManager.playOneShot("audios/clickUI")
  32. this.closeBtn.active =boolean
  33. this.openBtn.active =!boolean
  34. if(this.musicType ==MusicType.music ){
  35. audioManager.switchMusic = !boolean
  36. if(!boolean){
  37. audioManager.replay_music()
  38. }
  39. }
  40. if(this.musicType ==MusicType.sound ){
  41. audioManager.switchEffect = !boolean
  42. }
  43. if(this.musicType ==MusicType.vibrate ){
  44. audioManager.switch_vibrate = !boolean
  45. }
  46. audioManager.save()
  47. }
  48. clickOpen(){
  49. this.clickSwich(true)
  50. }
  51. clickClose(){
  52. this.clickSwich(false)
  53. }
  54. flash(open){
  55. this.openBtn.active = open
  56. this.closeBtn.active = !open
  57. }
  58. }