AudioMng.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const GameModule = require("../utils/GameModule");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. bgm: {
  6. tooltip: '背景音乐',
  7. default: null,
  8. type: cc.AudioClip
  9. },
  10. buttonClickAudio: {
  11. tooltip: '普通按钮点击音效',
  12. default: null,
  13. type: cc.AudioClip
  14. },
  15. getAwardAudio: {
  16. tooltip: '获得技能、奖励音效',
  17. default: null,
  18. type: cc.AudioClip
  19. },
  20. updateBuildingAudio: {
  21. tooltip: '升级或解锁建筑,购买明星音效',
  22. default: null,
  23. type: cc.AudioClip
  24. },
  25. clickCatAudio: {
  26. tooltip: '点击招财猫音效',
  27. default: null,
  28. type: cc.AudioClip
  29. },
  30. getDrawAudio: {
  31. tooltip: '抽奖音效',
  32. default: null,
  33. type: cc.AudioClip
  34. },
  35. getStarAudio: {
  36. tooltip: '获得明星音效',
  37. default: null,
  38. type: cc.AudioClip
  39. },
  40. volume: {
  41. tooltip: '总音量',
  42. default: 1,
  43. type: cc.Integer
  44. },
  45. bgmOpen: {
  46. get: function() {
  47. return this._bgmOpen;
  48. },
  49. set: function(value) {
  50. this._bgmOpen = value;
  51. if (this._bgmOpen) {
  52. this.playBgm();
  53. } else {
  54. this.stopBgm();
  55. }
  56. cc.sys.localStorage.setItem("game_bgm_state", value);
  57. }
  58. },
  59. soundEffectOpen: {
  60. get: function() {
  61. return this._soundEffectOpen;
  62. },
  63. set: function(value) {
  64. this._soundEffectOpen = value;
  65. cc.sys.localStorage.setItem("game_sound_effect_state", value);
  66. }
  67. }
  68. },
  69. onLoad () {
  70. GameModule.audioMng = this;
  71. this.currentAudio = {};
  72. this.stopAll();
  73. let bgmState = cc.sys.localStorage.getItem("game_bgm_state");
  74. if (bgmState!= undefined) {
  75. this.bgmOpen = bgmState;
  76. } else {
  77. this.bgmOpen = true;
  78. }
  79. let soundEffectState = cc.sys.localStorage.getItem("game_sound_effect_state");
  80. if (soundEffectState!= undefined) {
  81. this.soundEffectOpen = soundEffectState;
  82. } else {
  83. this.soundEffectOpen = true;
  84. }
  85. this.playBgm();
  86. },
  87. /**
  88. * 暂停所有音效
  89. */
  90. stopAll () {
  91. cc.audioEngine.stopAll();
  92. },
  93. /**
  94. * 播放背景音乐
  95. */
  96. playBgm () {
  97. cc.audioEngine.stop(this.currentAudio['bgm']);
  98. if (!this.bgmOpen) { return }
  99. this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
  100. },
  101. /**
  102. * 停止播放背景音乐
  103. */
  104. pauseBgm () {
  105. cc.audioEngine.pause(this.currentAudio['bgm']);
  106. },
  107. /**
  108. * 暂停播放背景音乐
  109. */
  110. stopBgm () {
  111. cc.audioEngine.stop(this.currentAudio['bgm']);
  112. },
  113. /**
  114. * 播放按钮音效
  115. */
  116. playClickButton () {
  117. if (!this.soundEffectOpen) { return }
  118. cc.audioEngine.play(this.buttonClickAudio, false, this.volume);
  119. },
  120. /**
  121. * 播放获得奖励音效
  122. */
  123. playGetAward () {
  124. if (!this.soundEffectOpen) { return }
  125. cc.audioEngine.play(this.getAwardAudio, false, this.volume);
  126. },
  127. /**
  128. * 播放升级建筑音效
  129. */
  130. playUpdateBuilding () {
  131. if (!this.soundEffectOpen) { return }
  132. cc.audioEngine.play(this.updateBuildingAudio, false, this.volume);
  133. },
  134. /**
  135. * 点击招财猫
  136. */
  137. playClickCat () {
  138. if (!this.soundEffectOpen) { return }
  139. cc.audioEngine.play(this.clickCatAudio, false, this.volume);
  140. },
  141. /**
  142. * 抽奖音效
  143. */
  144. playGetDraw () {
  145. if (!this.soundEffectOpen) { return }
  146. cc.audioEngine.play(this.getDrawAudio, false, this.volume);
  147. },
  148. /**
  149. * 获取到明星音效
  150. */
  151. playGetStar () {
  152. if (!this.soundEffectOpen) { return }
  153. cc.audioEngine.play(this.getStarAudio, false, this.volume);
  154. },
  155. /**
  156. * 停止播放获取明星音效
  157. */
  158. // pauseGetStar () {
  159. // this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
  160. // cc.audioEngine.pause(this.currentAudio['bgm']);
  161. // },
  162. // update (dt) {},
  163. });