优化音乐模块本地数据异常时,提供一套默认数据避免报错

This commit is contained in:
dgflash
2024-03-09 15:11:11 +08:00
parent 805280d741
commit f075f6ef65

View File

@@ -167,8 +167,7 @@ export class AudioManager extends Component {
this.local_data.switch_music = this._switch_music;
this.local_data.switch_effect = this._switch_effect;
let data = JSON.stringify(this.local_data);
oops.storage.set(LOCAL_STORE_KEY, data);
oops.storage.set(LOCAL_STORE_KEY, this.local_data);
}
@@ -177,25 +176,35 @@ export class AudioManager extends Component {
this.music = this.getComponent(AudioMusic) || this.addComponent(AudioMusic)!;
this.effect = this.getComponent(AudioEffect) || this.addComponent(AudioEffect)!;
let data = oops.storage.get(LOCAL_STORE_KEY);
if (data) {
this.local_data = oops.storage.getJson(LOCAL_STORE_KEY);
if (this.local_data) {
try {
this.local_data = JSON.parse(data);
this._volume_music = this.local_data.volume_music;
this._volume_effect = this.local_data.volume_effect;
this._switch_music = this.local_data.switch_music;
this._switch_effect = this.local_data.switch_effect;
this.setState();
}
catch (e) {
this.local_data = {};
this._volume_music = 1;
this._volume_effect = 1;
this._switch_music = true;
this._switch_effect = true;
this.setStateDefault();
}
if (this.music) this.music.volume = this._volume_music;
if (this.effect) this.effect.volume = this._volume_effect;
}
else {
this.setStateDefault();
}
if (this.music) this.music.volume = this._volume_music;
if (this.effect) this.effect.volume = this._volume_effect;
}
private setState() {
this._volume_music = this.local_data.volume_music;
this._volume_effect = this.local_data.volume_effect;
this._switch_music = this.local_data.switch_music;
this._switch_effect = this.local_data.switch_effect;
}
private setStateDefault() {
this.local_data = {};
this._volume_music = 1;
this._volume_effect = 1;
this._switch_music = true;
this._switch_effect = true;
}
}