Html5Overview Posted November 20, 2018 Share Posted November 20, 2018 Hi, id like to use different sword sounds on each click. I have 3 samples of a sword being drawn. What kind of road should I take to try and change this sound dynamically? Should I implement new sound on this Sound object each time I give it a click? https://streamable.com/5yktz Link to comment Share on other sites More sharing options...
thosakwe Posted November 20, 2018 Share Posted November 20, 2018 When you call "this.sound.add(...)," the return object is of type Phaser.Sound.BaseSound. Keep a reference to each of these sounds in your scene class. In fact, keep the sounds together in an array. Then, to pick a song to play, just choose a random index from the array, and call the play() method. Phaser has some utilities for picking a random item, IIRC, but otherwise you can just use Math.random. Link to comment Share on other sites More sharing options...
Html5Overview Posted November 20, 2018 Author Share Posted November 20, 2018 4 hours ago, thosakwe said: When you call "this.sound.add(...)," the return object is of type Phaser.Sound.BaseSound. Keep a reference to each of these sounds in your scene class. In fact, keep the sounds together in an array. Then, to pick a song to play, just choose a random index from the array, and call the play() method. Phaser has some utilities for picking a random item, IIRC, but otherwise you can just use Math.random. 1 I'm sorry, maybe I haven't explained it right. I want every click to sound different. So basically, sounds to change dynamically. I have tried before what you're saying and phaser did not switch sounds. It just create(){}'s random sound each time you reload game, but does not change sound dynamically. I will change sound in update() function just as I did background music. if(!this.music.isPlaying) { this.music = this.sound.add(SoundController.playSong(['song_1', 'song_2', 'song_3', 'song_4', 'song_5'])); this.music.play(); } Link to comment Share on other sites More sharing options...
thosakwe Posted November 20, 2018 Share Posted November 20, 2018 You should be calling this.sound.add, etc. in your create() method. And then just have a click listener that picks a random sound from an array (this should be a member on your class). I have no idea what's in your SoundController class, but calls to "this.sound.add" and "play()" are two different actions. See this gist for what I'm saying: https://gist.github.com/thosakwe/bade2c36c81f41b4a17e6482797dd598 Link to comment Share on other sites More sharing options...
Recommended Posts