setVolume

This commit is contained in:
oc 2018-05-05 01:54:57 +08:00
parent 9beec9f3ce
commit eafcfa4deb
6 changed files with 46 additions and 2 deletions

View File

@ -216,6 +216,7 @@ action
| pauseBgm_s | pauseBgm_s
| resumeBgm_s | resumeBgm_s
| playSound_s | playSound_s
| setVolume_s
| win_s | win_s
| lose_s | lose_s
| if_s | if_s
@ -834,6 +835,19 @@ var code = '{"type": "playSound", "name": "'+EvalString_0+'"},\n';
return code; return code;
*/ */
setVolume_s
: '设置音量' Int Newline
;
/* setVolume_s
tooltip : setVolume: 设置音量
helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setVolume-%e8%ae%be%e7%bd%ae%e9%9f%b3%e9%87%8f
default : [90]
colour : this.soundColor
var code = '{"type": "setVolume", "value": '+Int_0+'},\n';
return code;
*/
win_s win_s
: '游戏胜利,结局' ':' EvalString? Newline : '游戏胜利,结局' ':' EvalString? Newline
; ;
@ -1452,6 +1466,10 @@ ActionParser.prototype.parseAction = function() {
this.next = MotaActionBlocks['resumeBgm_s'].xmlText([ this.next = MotaActionBlocks['resumeBgm_s'].xmlText([
this.next]); this.next]);
break break
case "setVolume":
this.next = MotaActionBlocks['setVolume_s'].xmlText([
data.value, this.next]);
break
case "setValue": case "setValue":
this.next = MotaActionBlocks['setValue_s'].xmlText([ this.next = MotaActionBlocks['setValue_s'].xmlText([
MotaActionBlocks['idString_e'].xmlText([data.name]), MotaActionBlocks['idString_e'].xmlText([data.name]),

View File

@ -111,6 +111,7 @@ editor_blockly = function () {
MotaActionBlocks['pauseBgm_s'].xmlText(), MotaActionBlocks['pauseBgm_s'].xmlText(),
MotaActionBlocks['resumeBgm_s'].xmlText(), MotaActionBlocks['resumeBgm_s'].xmlText(),
MotaActionBlocks['playSound_s'].xmlText(), MotaActionBlocks['playSound_s'].xmlText(),
MotaActionBlocks['setVolume_s'].xmlText(),
'<label text="其他"></label>', '<label text="其他"></label>',
MotaActionBlocks['function_s'].xmlText(), MotaActionBlocks['function_s'].xmlText(),
], ],

View File

@ -842,6 +842,14 @@ move完毕后移动的NPC/怪物一定会消失只不过可以通过immediate
值得注意的是如果是额外添加进文件的音效则需在main.js中this.sounds里加载它。 值得注意的是如果是额外添加进文件的音效则需在main.js中this.sounds里加载它。
### setVolume设置音量
使用setVolume可以设置音量大小。
使用方法: `{"type": "setVolume", "value": 90}`
value为音量大小在0到100之间默认为100。设置后BGM和SE都将使用该音量进行播放。
### win: 获得胜利 ### win: 获得胜利
`{"type": "win", "reason": "xxx"}` 将会直接调用events.js中的win函数并将reason作为结局传入。 `{"type": "win", "reason": "xxx"}` 将会直接调用events.js中的win函数并将reason作为结局传入。

View File

@ -2110,6 +2110,7 @@ control.prototype.playBgm = function (bgm) {
} }
// 播放当前BGM // 播放当前BGM
core.musicStatus.playingBgm = bgm; core.musicStatus.playingBgm = bgm;
core.material.bgms[bgm].volume = core.musicStatus.volume;
core.material.bgms[bgm].play(); core.material.bgms[bgm].play();
core.musicStatus.isPlaying = true; core.musicStatus.isPlaying = true;
@ -2177,7 +2178,7 @@ control.prototype.playSound = function (sound) {
if (core.musicStatus.audioContext != null) { if (core.musicStatus.audioContext != null) {
var source = core.musicStatus.audioContext.createBufferSource(); var source = core.musicStatus.audioContext.createBufferSource();
source.buffer = core.material.sounds[sound]; source.buffer = core.material.sounds[sound];
source.connect(core.musicStatus.audioContext.destination); source.connect(core.musicStatus.gainNode);
try { try {
source.start(0); source.start(0);
} }
@ -2190,11 +2191,12 @@ control.prototype.playSound = function (sound) {
} }
} }
else { else {
core.material.sounds[sound].volume = core.musicStatus.volume;
core.material.sounds[sound].play(); core.material.sounds[sound].play();
} }
} }
catch (eee) { catch (eee) {
console.log("无法播放SE "+bgm); console.log("无法播放SE "+sound);
console.log(eee); console.log(eee);
} }
} }

View File

@ -48,6 +48,8 @@ function core() {
'soundStatus': true, // 是否播放SE 'soundStatus': true, // 是否播放SE
'playingBgm': null, // 正在播放的BGM 'playingBgm': null, // 正在播放的BGM
'isPlaying': false, 'isPlaying': false,
'gainNode': null,
'volume': 1.0, // 音量
} }
this.platform = { this.platform = {
'isOnline': true, // 是否http 'isOnline': true, // 是否http
@ -187,6 +189,8 @@ core.prototype.init = function (coreData, callback) {
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;
try { try {
core.musicStatus.audioContext = new window.AudioContext(); core.musicStatus.audioContext = new window.AudioContext();
core.musicStatus.gainNode = core.musicStatus.audioContext.createGain();
core.musicStatus.gainNode.connect(core.musicStatus.audioContext.destination);
} catch (e) { } catch (e) {
console.log("该浏览器不支持AudioContext"); console.log("该浏览器不支持AudioContext");
core.musicStatus.audioContext = null; core.musicStatus.audioContext = null;

View File

@ -537,6 +537,17 @@ events.prototype.doAction = function() {
core.resumeBgm(); core.resumeBgm();
this.doAction(); this.doAction();
break break
case "setVolume":
data.value = parseInt(data.value||0);
if (data.value>100) data.value=100;
data.value = data.value / 100;
core.musicStatus.volume = data.value;
if (core.isset(core.musicStatus.playingBgm)) {
core.material.bgms[core.musicStatus.playingBgm].volume = data.value;
}
core.musicStatus.gainNode.gain.value = data.value;
this.doAction();
break;
case "setValue": case "setValue":
try { try {
var value=core.calValue(data.value); var value=core.calValue(data.value);