In this guide, we will be learning how to load sounds with Phaser 3. At this point it would be useful if you had a scene already setup. If you don’t, you can take a look at my boilerplate code guide in this series.
In your scene, define a preload method if you haven’t already. In order to define an image, you will need to follow this syntax:
Realistically, this would look more like the following:
In this method call, key is the name you wish to reference you sound within your code. The second argument should contain the path to your sound, including the file extension.
Try loading a sound by yourself. If you have a create method in your scene, you can add a sound and reference the key you defined. We can use the this.add.sound method and assign the returned sound to a variable or property.
this.mySound = this.sound.add("mysound");
Whenever you want to play the sound within your code, ensure you’re calling the following method after the sound was defined above. Write the following line to play the sound:
With that, you now know how to play sounds with Phaser 3! If you found this guide valuable, you can find the rest of the articles in this series here. If you would like to receive news regarding future tutorials and courses we publish, you can subscribe to our newsletter here. You can also share this guide on social media, it really helps and is much appreciated; of course, only if you found this guide helpful.