

This is a non-visual component that represents a sound file with some playback parameters. TCastleSound: The most important class, you should use this always when you want to play any sound. The most important classes and their usage

You just saw how to add audio to your Phaser 3.x game. If you had background music, you could just play the audio on loop in the createScene function. If you look at each of the collider methods, you’ll notice that we are calling ay() and ay() to play our sounds. Instead it needs to be triggered kind of like how an animation is played on a sprite. However, for both audio files, we’ve added them to our scene and defined that they should not be looped when played.Īdding the audio to the scene does not automatically play it. You’ll notice that I’ve commented out the code for animations and the adding of our sprites because we’ve already seen it. On your computer, create a new directory with an index.html file that contains the following HTML markup: Create a New Phaser 3.x Game with Boilerplate Codeīefore we get into the audio side of things, we need to have a project to work from. We’re not going to worry about object pooling and similar. When the player collides with each game object, a different sound will play. What we’re going to do is have an animated player sprite and two other game objects. We’re going to slim the example down a bit to avoid any unnecessary complexities in the tutorial. If you didn’t hear any bell noises or explosion noises, make sure your speakers are on and that your browser allows for the output of sound. The above video takes the topics that we explored in previous tutorials and adds audio to them. To get a better idea of what we want to accomplish, take a look at the following video: Your browser does not support the audio element. In this tutorial, we’re going to see how to add audio to our Phaser 3.x game in the form of sound effects triggered by collisions. We’ve seen everything from adding and animating sprites, to handling collisions between game objects.

I’ve recently written quite a few tutorials on the topic of game development with Phaser. Without audio, the gameplay experience can feel quite boring, which no one wants their game to be labeled as. If you’re building a game, you’re probably going to need audio such as sound effects and background music at some point in time.
