From bd2340168cac2d03ea11369089192a41ff4316c2 Mon Sep 17 00:00:00 2001 From: oc Date: Sat, 7 Jul 2018 02:11:41 +0800 Subject: [PATCH] setVolume fade --- _server/blockly/MotaAction.g4 | 9 ++++---- docs/element.md | 2 ++ docs/event.md | 4 +++- libs/events.js | 43 +++++++++++++++++++++++++++++------ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/_server/blockly/MotaAction.g4 b/_server/blockly/MotaAction.g4 index 403d19d9..ab728be1 100644 --- a/_server/blockly/MotaAction.g4 +++ b/_server/blockly/MotaAction.g4 @@ -927,15 +927,16 @@ return code; */; setVolume_s - : '设置音量' Int Newline + : '设置音量' Int '渐变时间' Int? Newline /* setVolume_s tooltip : setVolume: 设置音量 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setvolume%EF%BC%9A%E8%AE%BE%E7%BD%AE%E9%9F%B3%E9%87%8F -default : [90] +default : [90, 500] colour : this.soundColor -var code = '{"type": "setVolume", "value": '+Int_0+'},\n'; +Int_1 = Int_1?(', "time": '+Int_1):"" +var code = '{"type": "setVolume", "value": '+Int_0+Int_1+'},\n'; return code; */; @@ -1641,7 +1642,7 @@ ActionParser.prototype.parseAction = function() { break case "setVolume": this.next = MotaActionBlocks['setVolume_s'].xmlText([ - data.value, this.next]); + data.value, data.time, this.next]); break case "setValue": this.next = MotaActionBlocks['setValue_s'].xmlText([ diff --git a/docs/element.md b/docs/element.md index 1a9bbf28..a82be0ec 100644 --- a/docs/element.md +++ b/docs/element.md @@ -193,6 +193,8 @@ floorId指定的是目标楼层的唯一标识符(ID)。 导出动画时可能会进行一些压缩以节省流量,因此清晰度可能不如原版。 +从2.3.2开始,动画可以同时导出所用的音效。**如果导出音效,请确保将所用到的音效复制到了`sounds`目录下,并且在全塔属性中注册过。** + 动画播放时,是按照每秒20帧的速度(即50ms/帧)。 定义完毕后,我们可以调用`animate`事件来播放该动画,有关事件的详细介绍请参见[事件](event)。 diff --git a/docs/event.md b/docs/event.md index 3e83793f..1bf94a19 100644 --- a/docs/event.md +++ b/docs/event.md @@ -939,10 +939,12 @@ time选项为该跳跃所需要用到的时间。 使用setVolume可以设置音量大小。 -使用方法: `{"type": "setVolume", "value": 90}` +使用方法: `{"type": "setVolume", "value": 90, "time": 500}` value为音量大小,在0到100之间,默认为100。设置后,BGM和SE都将使用该音量进行播放。 +可以设置time为音量渐变时间。 + ### win:获得胜利 `{"type": "win", "reason": "xxx"}` 将会直接调用events.js中的win函数,并将reason作为结局传入。 diff --git a/libs/events.js b/libs/events.js index f8ff8424..da973777 100644 --- a/libs/events.js +++ b/libs/events.js @@ -654,14 +654,11 @@ events.prototype.doAction = function() { break case "setVolume": data.value = parseInt(data.value||0); + if (data.value<0) 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(); + this.setVolume(data.value/100, data.time, function() { + core.doAction(); + }); break; case "setValue": try { @@ -1272,6 +1269,38 @@ events.prototype.moveImage = function (image, from, to, time, callback) { }, time / 64); } +////// 淡入淡出音乐 ////// +events.prototype.setVolume = function (value, time, callback) { + + var set = function (value) { + core.musicStatus.volume = value; + if (core.isset(core.musicStatus.playingBgm)) { + core.material.bgms[core.musicStatus.playingBgm].volume = value; + } + core.musicStatus.gainNode.gain.value = value; + } + + if (!core.isset(time) || time<100) { + set(value); + if (core.isset(callback)) callback(); + return; + } + core.status.replay.animate=true; + var currVolume = core.musicStatus.volume; + var step = 0; + var fade = setInterval(function () { + step++; + var nowVolume = currVolume+(value-currVolume)*step/32; + set(nowVolume); + if (step>=32) { + clearInterval(fade); + core.status.replay.animate=false; + if (core.isset(callback)) + callback(); + } + }, time / 32); +} + ////// 打开一个全局商店 ////// events.prototype.openShop = function(shopId, needVisited) { var shop = core.status.shops[shopId];