KaBoom-audio

参考

参考官方的范例 audio examples

曲子特别好听和可爱

功能

在开始之前需要加载目录和声音文件

loadRoot("/pub/sounds/");
loadSound("wooosh", "wooosh.mp3");
loadSound("OtherworldlyFoe", "OtherworldlyFoe.mp3");

然后在场景中直接进行播放,调用pause和resume / stop 进行播放暂停还有停止

const music = play("OtherworldlyFoe", { loop: true, });
keyPress("space", () => {
    if (music.paused()) {
        music.resume();
    } else {
        music.pause();
    }
    updateText(); 
    play("wooosh");
});
keyPress("escape", () => {
        music.stop();
        updateText();
});

可以动态修改音乐的响度以及通过加减速进行变调

keyPress("down", () => {
        music.volume(music.volume() - 0.1);
        updateText();
});
keyPress("left", () => {
        music.detune(music.detune() - 100);
        updateText();
});

一些可能会不认识的语法

function updateText() {
        label.text = `
${music.paused() ? "paused" : "playing"}
volume: ${music.volume()}
detune: ${music.detune()}
        `.trim();
}

${ } 写法被称作模版字符串 Template strings 在其中起到tostring()类型转换的作用,把js变量放进字符串的语法,可以看作想做 toString() 只是${}语法可以让你把字符串和变量写在一起, 比如 "oh hi " + name简写成 oh hi ${name}${} 只存在于字符串引号里面。

Trim 的作用是 去除字符串左右两端的空格 The trim() method removes whitespace from both sides of a string.

最后更新于