Skip to content

Instantly share code, notes, and snippets.

@samme
Last active February 21, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samme/b959e2b812dd26b92d5f3ded8e62449c to your computer and use it in GitHub Desktop.
Save samme/b959e2b812dd26b92d5f3ded8e62449c to your computer and use it in GitHub Desktop.
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