Phaser for Beginners

Preloading sounds

When the player chooses to play with sounds, it means the game should feature sounds. Crack Alien Code will have three sounds: one to be played each time a tile is selected, one to be played when the player makes a successful match and one to be played when the player makes an unsuccessful match.

I added in my game folder three new sounds, in two different formats: mp3 and ogg and now the folder looks like this:

Why did I use two sound formats?

It’s a compatibility matter: not all browsers are capable to reproduce all kind of sound files. Using mp3 and ogg together should grant the best device and browser coverage.

Preloading sounds is not different than preloading images, as you can see in preload function in playGame object:

Phaser will choose which sound format to play according to browser capabilities.

load.audio(key, audioFiles) handles sound preloading. The first argument is the key, the second is an array of files to be loaded, in different formats.

With sounds ready to be played, it’s time to see how we can reproduce them.

Download Source

File: preloading-sounds.zip


Next Tutorial →

Playing sounds

← Previous Tutorial

Adding a title screen with sound/mute options

Leave a Reply

Your email address will not be published. Required fields are marked *