O'Reilly logo

Haxe Game Development Essentials by Jeremy McCurdy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding music

Now we're going to add functions to play our music tracks and then add the calls to play them in the game.

Creating playback functions

In SoundManager, add the function to play our fanfare music:

public function playFanfareMusic():Void{
    FlxG.sound.playMusic(Sounds.MUSIC_FANFARE,  Sounds.VOLUME_FANFARE);
}

This function will call the playMusic function on the sound object of FlxG. This will stream and play a music clip that will loop by default. We pass in two arguments: the first is the ID of the music to play and the second is the volume to play it at. We pull both of these from the constants that were made earlier.

Next let's make the function to play the in game music:

public function playInGameMusic():Void{ FlxG.sound.playMusic(Sounds.MUSIC_INGAME, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required