Created
November 20, 2018 21:05
-
-
Save thosakwe/bade2c36c81f41b4a17e6482797dd598 to your computer and use it in GitHub Desktop.
Random sounds in Phaser
This file contains 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
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