Sign in to follow this  
Html5Overview

Change sound on click dynamically

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.

Share this post


Link to post
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();
}

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.