Jump to content

Change sound on click dynamically


Html5Overview
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...