How to add looping audio in Phaser 3.60

Hey Wizardy Game Devs! Today, you will learn how to add a looping audio to your Phaser 3.60 Game.
Firstly, you will need to save the music file in your project directory. Then, you will need to import the file to your project by adding the following line of code.
import looping_audio from './path/to/looping/audio';
After that, You will need to load the file in your project using the preload()
function.
preload() {
this.load.audio('looping_audio', looping_audio);
}
Now, in your create()
function, add the audio asset ( looping_audio
) to a new sound object named myAudio
. For making it loop continuously, set the loop property of looping_audio
to true. Then, start playing the audio.
create() {
this.myAudio = this.sound.add('looping_audio');
this.myAudio.loop = true;
this.myAudio.play();
}
And with this, your audio will be played forever. You can add this looping property to the background music of your game to bring up an immersive atmosphere in the game.
Happy Coding! 🔥
Subscribe to my newsletter
Read articles from Damek Whiz directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Damek Whiz
Damek Whiz
A Game Dev whiz on a quest to Learn, Create and Share.