Files
fc/dev/project/library/imports/35/35bf7692-c730-42f4-9547-2cd7c474723d.js
T

211 lines
4.6 KiB
JavaScript
Raw Normal View History

2026-05-24 10:21:26 +08:00
"use strict";
cc._RF.push(module, '35bf7aSxzBC9JVHLNfEdHI9', 'nx.fx.BGM');
// Scripts/nx/cmp/flex/nx.fx.BGM.js
"use strict";
/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* NX UI插件 -- 背景音乐
*
* 2018.05.18
******************************************************************/
cc.Class({
"extends": cc.Component,
properties: {
bgmRK: {
"default": "",
displayName: "资源键"
},
isFadeIn: {
"default": true,
displayName: "渐入"
},
isFadeOut: {
"default": true,
displayName: "渐出"
},
fadeSecs: {
"default": 0.5,
displayName: "出入耗时"
},
isMainBGM: {
"default": false,
displayName: "主组件"
},
restoreLastBGM: {
"default": true,
displayName: "关闭恢复上次音效"
}
},
// 编辑器特性
editor: {
// 允许当前组件在编辑器模式下运行
executeInEditMode: false,
// requireComponent 参数用来指定当前组件的依赖组件
requireComponent: null,
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
disallowMultiple: true,
// menu 用来将当前组件添加到组件菜单中,方便用户查找
menu: "扩充/audio.bgm 背景音乐"
},
// 初始化
onEnable: function onEnable() {
// 重置
this.reset();
// 激活
this.reActive();
// 关闭所有当前音效
nx.audio.stopSFXAll();
// 主组件
if (this.isMainBGM) {
nx.bridge.cbgms = nx.bridge.cbgms || [];
nx.bridge.cbgms.push(this);
}
},
// 隐藏
onDisable: function onDisable() {
var _this = this;
// 重置
this.reset();
// 主组件
if (this.isMainBGM) {
nx.dt.arrDelete(nx.bridge.cbgms, function (_tm) {
return _tm == _this;
});
}
// 恢复
if (this.restoreLastBGM) {
if (nx.dt.arrNEmpty(nx.bridge.cbgms)) {
var bgm = nx.bridge.cbgms[nx.bridge.cbgms.length - 1];
if (bgm) {
bgm.reActive();
}
} else {
nx.audio.stopBGM();
}
}
},
// 重置
reset: function reset() {
this.loading = false;
this.fading = 0;
this.bgmKey = "";
},
// 激活
reActive: function reActive() {
// 无效
if (nx.dt.strEmpty(this.bgmRK)) {
return;
}
// 进行中
if (this.loading || this.fading != 0) {
nx.error("[BGM]激活失败,进行中!");
return;
}
this.loading = true;
this.fading = 0;
// 预载入音乐
var self = this;
nx.res.loadAudio(this.bgmRK, function (_error, _params) {
self.loading = false;
// 淡出
if (!_error) {
self.musicClip = _params;
self.fadeOut();
}
});
},
// 淡出原有音乐
fadeOut: function fadeOut() {
var vol = nx.audio.getBGMVolume();
this.rawVol = vol;
this.curVol = vol;
this.vps = 1 / this.fadeSecs;
// this.bgmKey = this.musicClip;
//没有当前数据
if (!this.musicClip) {
nx.error("[BGM]播放失败,没有相关音频!", this.bgmRK);
}
// 没有当前音乐
if (nx.dt.strEmpty(nx.audio.bgmRes)) {
this.fadeIn();
return;
}
// 未开启/当前音量为0
if (!this.isFadeOut || vol <= 0) {
this.fadeIn();
return;
}
// 淡出
this.fading = 1;
},
// 淡入新音乐
fadeIn: function fadeIn() {
// 关闭当前
nx.audio.stopBGM();
// 播放新的
this.bgmKey = nx.audio.playBGM(this.musicClip);
// 未开启/目前音量为0
if (!this.isFadeOut || this.rawVol <= 0) {
this.fading = 0;
nx.audio.setBGMVolume(this.rawVol, true);
return;
}
// 淡入
this.fading = 2;
this.curVol = 0;
},
// 更新
update: function update(_dt) {
// 未开启
if (this.fading != 1 && this.fading != 2) {
return;
}
// 音乐已改变
// if( this.bgmKey != this.lastBgmKey ) {
// this.reset();
// return;
// }
// 淡出
if (this.fading == 1) {
this.curVol = Math.max(0, this.curVol - this.vps * _dt);
nx.audio.setBGMVolume(this.curVol, true);
if (this.curVol <= 0) {
this.fadeIn();
return;
}
}
// 淡入
if (this.fading == 2) {
this.curVol = Math.min(this.rawVol, this.curVol + this.vps * _dt);
nx.audio.setBGMVolume(this.curVol, true);
if (this.curVol >= this.rawVol) {
this.fading = 0;
return;
}
}
}
});
cc._RF.pop();