Notice I have version four of the game that includes audio. And it's quite simple to use audio, use the classes that are included in the game.js JavaScript file. Let's just have a look how we use them. Notice that in the init method, we call a method of the game called init. sf acts as effects for sound effects. And if we look at the init method, we initially create an S effects object.
And then we give it a context. This is very important And you need to just use window the audio context or window up WebKit audio context, that will ensure that you get an older context that suits your browser. We then create a list of sound effects that we're going to use in our game. If you look in the assets as effects folder, you'll see that there are multiple versions of each sound. That's because some browsers support mp3 and some browsers support or GG depends on your browser. And we want to ensure that everybody can hear the sound.
We then go through our list using the for each method. And the function callback there will be given each one of these items in turn And then we define a sound effect based on that name, calling new s effects. Using this as the parameters needs, the context needs a source passing in an mp3 and an O g, g g pass, whether or not it loops, whether or not it also plays that will, autoplay will never work on mobile devices, there has to be a user click before any sound will be will be activated. And setting the volume to naught point three volume can be between zero and one, zero being silent one being full volume. So that's using the SF x class, which is part of the game JavaScript file that you'll find in your resources that we've been looking at throughout the The course is the SF x.
First of all, it uses the context from the options then defines the volume which defaults to one then creates a gain node don't really need to know about this, but it's but it that's what he does and it sets the value to the current volume and then connects it to the context and then he decides whether or not it loops whether it fades in whether or not it also plays and sets our buffer to null. We then check our codecs. And we look we only load in the suitable codec so so that effect there will be To create an extension value. So whether it's mp3 or whether it's Oh GG. And we use this to set our URL based on the source has been passed in. And then we load our particular sound.
This is the load method. Again, it's unnecessary for you to understand this in detail, but it's using an XML HTTP request. And it's also a plane then once it's all loaded in it creates a buffer. So stores the buffer, and then plays it. Important things that you'll be using our play, which allows us to start playing the sound effects and volume, which allows us to set the volume of the sound effects Also, you probably also use stop. And maybe you use pause, then to actually use this at the sound.
There's an example. We just do this stuff asset x because x is an object of this game, this game, the name of the sound effects and then play. It's as simple as that. That allows us to add simple sound effects to our game. could even be a looping music track. We have multi layered sounds and we can set the individual volume of each sound effect