Last active
February 21, 2020 19:35
-
-
Save samme/b959e2b812dd26b92d5f3ded8e62449c to your computer and use it in GitHub Desktop.
Switch audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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