Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Created November 20, 2018 21:05
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 thosakwe/bade2c36c81f41b4a17e6482797dd598 to your computer and use it in GitHub Desktop.
Save thosakwe/bade2c36c81f41b4a17e6482797dd598 to your computer and use it in GitHub Desktop.
Random sounds in Phaser
import {Scene} from 'phaser';
export default class extends Scene {
private sounds: Phaser.Sound.BaseSound[] = [];
create() {
this.sounds.push(this.sound.add('song_1', '<url>', etc...));
this.sounds.push(this.sound.add('song_2', '<url>', etc...));
this.sounds.push(this.sound.add('song_3', '<url>', etc...));
this.sounds.push(this.sound.add('song_4', '<url>', etc...));
}
update() {
if (this.input.whateverButton.isDown) {
// Pick a random sound
var index = Math.round(Math.random() * this.sounds.length);
var sound = this.sounds[index];
// Play it
sound.play();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment