How do i code music

I have looked in the most obvious spots like online and the documentations tab but I can't find a tutorial on how to play music in my game. A link to a video or informational sheet would also help. Thanks

It's really quite easy and maybe that's why there is no tutorial. Simply load your music as .wav or .mp3 into the music resource, that's a tab on the left. then code this "audio.playMusic("yourmusic", 1, 1)" to play it at full volume, and loop continually. You can place it in the init function.

When you play the music, you can also set it as a variable. eg. myMusic = audio.playMusic("yourmusic", 1, 1). The second parameter in the function is the volume the music plays at, and the third is a true or false (1 or 0) value of weather or not it loops. The variable that the music is set to becomes an object with several functions and variables, including:

myMusic.setVolume(volume) //sets the volume, value between 1 and 0
myMusic.stop() //stops the music
myMusic.play() //starts the music (assuming it was previously stopped)
myMusic.getPosition() //the position in the song that the music is up to
myMusic.getDuration() //the total duration of the music in seconds

In the doc, you can find the sounds heading with more information on how music works.

Note: the playMusic function assumes that the input is the name of an .mp3 file. For a .wav file, use the audio.playSound() function instead (same paramaters, returns pretty much the same object but with different functions that are explained in the doc)

Post a reply



Validate your e-mail address to participate in the community