Playing With Sound and Music

May 17, 2022 Posted by zachary

In Chapters 12 and 13 of Beginning Game Programming, we learned how to add sound and music to games. Thankfully, HaxeFlixel makes doing so easy. The projects for these chapters were to just add sound and music to two existing games, Memory and Dino Cross. So I did that, but I also made a little toy to play with some other sound and music options.

Now, if you just want to add sound and music, it is relatively simple. To add a sound, you will need to do the following.

//We need to include the sound API
import flixel.system.FlxSound;
//Create our sound variable
var tickSound:FlxSound;
//Load the sound we want to play. You can set some other options here as well, such as the volume in this example.
tickSound = FlxG.sound.load(AssetPaths.tick__wav, 0.4);
//Call the play function;

There are a lot of other options you can set, such as looping or adjusting the pan or amplitude of the sound. But for most uses, this is all you need.

Moving on to music, it is also just as simple.

//Make sure to include FlxG
import flixel.FlxG;
//Call the playMusic function
//Parameters are the song, the volume, whether to loop.
FlxG.sound.playMusic(AssetPaths.amazingmazes__ogg, 0.5, true);

That is it. With the music looping, you will have music playing all over. Also, if you set the music to play in the first state you load, such as your Menu/Title screen, it wil lcontinue playing in all states after until you stop it or play a different track.

So, with this knowledge, I added sound effects to the three full games I have made so far, Memory, Unicorn Dash, and Dino Cross. I have also added a simple song to Dino Cross. But that wasn’t sufficient enough for me. I wanted to do something that showed off sound a little better.

Creating the Sound Board

Once I added sound and music to my games, I created a little sound board that shows off some of the features of sound and music. This board looks pretty simple on the surface, but it was actually one of the most complicated due to the number of objects I have created.

Nearly everything is interactive, Each object performs a different action. I will leave you to explore what happens. Just have fun.


Going forward, all the games I create will include sound and music. The next project will be a whack-a-mole style game. It should be a lot of fun to make. You can check out the the completed games and the soundboard over on itch. You can check out the code over on Git.

Leave a Reply

Your email address will not be published. Required fields are marked *