Adding background music to a game gives a lot of advantages. You can really set the tone of the kind of game you are creating.
Adding a soundtrack has some similar elements to adding a sound effect which we did in another chapter;
We can add a sound track to our game to act as a bit of background music or a sound effect while we play. For this demo let's just add a noise that lasts for 2 seconds.
To do this we load a new sound to our project in preload with the following linke
game.load.audio('backgroundnoise' , 'https://cdn.glitch.com/04f51d70-ab26-4960-8e87-12f2aa251cff%2Fbackground_noise.wav');
Then we can create a variable with that loaded noise with the following lines in our playState.create function
backgroundnoise = game.sound.add("backgroundnoise", 1, true);
The game.sound.add() function here has three parameters.
The default for the volume parameter is level 1 (100%), and the default value for loop is false, so it only plays once.
So in this case we have to include a value for the volume parameter if we want to set the third loop parameter to true or the function will get confused.
There has been error in communication with Booktype server. Not sure right now where is the problem.
You should refresh this page.