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.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

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.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-04-21T05:00:00+00:00) Phaser: Playing sounds. Retrieved from https://www.scien.cx/2021/04/21/phaser-playing-sounds/

MLA
" » Phaser: Playing sounds." flaviocopes.com | Sciencx - Wednesday April 21, 2021, https://www.scien.cx/2021/04/21/phaser-playing-sounds/
HARVARD
flaviocopes.com | Sciencx Wednesday April 21, 2021 » Phaser: Playing sounds., viewed ,<https://www.scien.cx/2021/04/21/phaser-playing-sounds/>
VANCOUVER
flaviocopes.com | Sciencx - » Phaser: Playing sounds. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/21/phaser-playing-sounds/
CHICAGO
" » Phaser: Playing sounds." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/04/21/phaser-playing-sounds/
IEEE
" » Phaser: Playing sounds." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/04/21/phaser-playing-sounds/. [Accessed: ]
rf:citation
» Phaser: Playing sounds | flaviocopes.com | Sciencx | https://www.scien.cx/2021/04/21/phaser-playing-sounds/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.