|
|
@@ -14,35 +14,16 @@ export default class AudioSystem {
|
|
|
private _effectVolume: number = 1;
|
|
|
/** 音乐音量 */
|
|
|
private _musicVolume: number = 1;
|
|
|
- /** 暂时音效开关 */
|
|
|
- private _switchTempEffect: boolean = true;
|
|
|
/** 音频map */
|
|
|
private _map_audio: Map<AUDIO_NAME, cc.AudioClip> = new Map();
|
|
|
|
|
|
+ private _bg_audio:cc.AudioClip;
|
|
|
+
|
|
|
/** 初始化 */
|
|
|
public init() {
|
|
|
// 获取本地开关设置
|
|
|
- let effect = cc.sys.localStorage.getItem("switchEffect");
|
|
|
- if (effect) {
|
|
|
- if (effect == "true") {
|
|
|
- this._switchEffect = true;
|
|
|
- } else {
|
|
|
- this._switchEffect = false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- let music = cc.sys.localStorage.getItem("switchMusic");
|
|
|
- if (music) {
|
|
|
- if (music == "true") {
|
|
|
- this._switchMusic = true;
|
|
|
- } else {
|
|
|
- this._switchMusic = false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (this._switchMusic) {
|
|
|
- this.playMusic('bgm', true)
|
|
|
- }
|
|
|
+ this._switchEffect = !(cc.sys.localStorage.getItem("switchEffect") == "false");
|
|
|
+ this._switchMusic = !(cc.sys.localStorage.getItem("switchMusic") == "false");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -51,12 +32,13 @@ export default class AudioSystem {
|
|
|
* @param loop 是否循环【默认循环】
|
|
|
*/
|
|
|
public async playMusic(audio_name: AUDIO_NAME, loop: boolean = true) {
|
|
|
- let asset = this._map_audio.get(audio_name);
|
|
|
- if (!asset) {
|
|
|
- asset = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
|
|
|
- this._map_audio.set(audio_name, asset);
|
|
|
+ if(!this._switchMusic){
|
|
|
+ return;
|
|
|
}
|
|
|
- cc.audioEngine.playMusic(asset, loop);
|
|
|
+ if (!this._bg_audio) {
|
|
|
+ this._bg_audio = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
|
|
|
+ }
|
|
|
+ cc.audioEngine.playMusic(this._bg_audio, loop);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -65,14 +47,15 @@ export default class AudioSystem {
|
|
|
* @param loop 是否循环【默认不循环】
|
|
|
*/
|
|
|
public async playEffect(audio_name: AUDIO_NAME, loop: boolean = false) {
|
|
|
- if (this._switchEffect && this._switchTempEffect) {
|
|
|
+ if (this._switchEffect) {
|
|
|
let asset = this._map_audio.get(audio_name);
|
|
|
if (!asset) {
|
|
|
asset = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
|
|
|
this._map_audio.set(audio_name, asset);
|
|
|
}
|
|
|
- cc.audioEngine.playEffect(asset, loop);
|
|
|
+ return cc.audioEngine.playEffect(asset, loop);
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -136,22 +119,15 @@ export default class AudioSystem {
|
|
|
cc.audioEngine.stopAllEffects();
|
|
|
}
|
|
|
|
|
|
- /** 暂时开启关闭音效开关
|
|
|
- * @param state true 打开 false 关闭
|
|
|
- */
|
|
|
- public setTempEffect(state: boolean) {
|
|
|
- this._switchTempEffect = state;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 转换音乐按钮开关
|
|
|
*/
|
|
|
public switchMusicFunc() {
|
|
|
this._switchMusic = !this._switchMusic;
|
|
|
if (!this._switchMusic) {
|
|
|
- this.stopMusic();
|
|
|
+ this.setPauseMusic();
|
|
|
} else {
|
|
|
- this.playMusic('bgm');
|
|
|
+ this.setResumeMusic();
|
|
|
}
|
|
|
cc.sys.localStorage.setItem("switchMusic", this._switchMusic.toString());
|
|
|
}
|