Hey Wizardy Game Devs! Today, you will learn how to kick in some background music 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 bg_music from './path/to/background-music';
After that, You will need to load the file in your project using the preload()
function.
preload() {
this.load.audio('bg_music', bg_music);
}
Now, in your create()
function, add the audio asset ( bg_music
) to a new sound object named bgMusic
. To make sure the background music plays continuously, set the loop property of bgMusic
to true. Then, start playing the background music.
create() {
this.bgMusic = this.sound.add('bg_music');
this.bgMusic.loop = true;
this.bgMusic.play();
}
🎉Voila! With this, your background music will be played forever, captivating players and inviting them to immerse themselves in your game's enchanting atmosphere.