Skip to content

Phaser: Playing sounds

This post is part of a Phaser series. Click here to see the first post of the series.

Similar to displaying images, Before you can play an audio file, you must preload it and assign it to a label:

function preload() {
  this.load.audio('sound', 'sound.mp3')
}

For images we used this.load.image(), here we use this.load.audio().

Once this is done, we can use the sound into our create() or update() functions:

this.sound.add('sound')

This will get you back an object. It’s important to assign it to a variable:

const sound = this.sound.add('sound')

Because later, when we want, we’ll call the play() method on it:

sound.play()

You can combine this with mouse events, for example, to play a sound when an item is clicked or hovered.

I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list