Skip to content

Instantly share code, notes, and snippets.

@samme
Last active Feb 21, 2020
Embed
What would you like to do?
Switch audio
var music = [
{
key: 'bgMusic',
url: 'assets/TownTheme.mp3',
config: {
volume: 0.1,
loop: true
}
},
{
key: 'lvl1Music',
url: 'assets/TownTheme2.mp3',
config: {
volume: 0.1,
loop: true
}
},
{
key: 'lvl1MusicA',
url: 'assets/bgm/lvl-1/SMILE.mp3',
config: {
volume: 0.03,
loop: true
}
},
{
key: 'lvl2Music',
url: 'assets/bgm/lvl-1/smb-overworld-remix.mp3',
config: {
volume: 0.1,
loop: true
}
}
];
var sfx = [
{
key: 'jumpA',
url: 'assets/bgm/lvl-1/SMILE.mp3',
config: {
volume: 0.1,
loop: true
}
}
];
var currentSound = null;
function preload() {
this.load.audio(music);
this.load.audio(sfx);
}
function create() {
switchAudio.call(this, music[0]);
}
function switchAudio(record) {
// `this` is the scene
if (currentSound) {
currentSound.stop();
this.sound.remove(currentSound);
}
currentSound = this.sound.add(record.key, record.config);
currentSound.play();
}
new Phaser.Game({
scene: {
preload: preload,
create: create
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment