Music
Music plays one looping track at a time. You name the track you want, and it stops whatever was playing before. A track can have an intro that plays once before the rest of it repeats, and the jump back is seamless. Game creates one from its Sounds and exposes it as services.music in the games that use it.
Quick Start
Section titled “Quick Start”List your tracks once, usually in src/config/music.js:
export const Track = { // Plays from the start, then loops back to 5.598 s every time it ends. Theme: { sound: 'theme', loopStart: 5.598 }, // Loops the whole file. Ambience: { sound: 'ambience' },};Then ask for a track wherever it should start:
this.game.music.play(Track.Theme);Each sound is the name of a loaded sound definition. Music reads that definition’s path and volume:
const soundDefinitions = [ { name: 'theme', path: './assets/sounds/theme.ogg', size: 1, volume: 0.3 }, { name: 'ambience', path: './assets/sounds/ambience.ogg', size: 1, volume: 0.3, },];flowchart
A["music.play(track)"] --> B{Same track?}
B -- yes --> C[Keep playing]
B -- no --> D[Stop the old track]
D --> E{"Has the player clicked\nor pressed a key yet?"}
E -- no --> F[Wait for the first input]
F --> G
E -- yes --> G[Play the file from 0 s]
G --> H[Reach loopEnd]
H --> I[Jump back to loopStart]
I --> H
Tracks
Section titled “Tracks”| Track field | Meaning |
|---|---|
sound |
Name of the sound definition holding the file and its volume. |
loopStart |
Optional; seconds into the file where each repeat starts. Defaults to 0, so the whole file repeats. |
loopEnd |
Optional; seconds into the file where each repeat jumps back. Defaults to the end of the file. |
Everything before loopStart is the intro, and it plays once. Everything after loopEnd never plays, so most files are simply cut at the loop’s end, and only loopStart is needed.
Methods
Section titled “Methods”play(track)
Section titled “play(track)”music.play(Track.Battle);Stops the current track and starts track from the beginning. Asking for the track that’s already playing does nothing, so it’s safe to call every frame, for example from a state’s update().
stop()
Section titled “stop()”music.stop();Stops the current track. Playing it again starts it from the beginning.
pause() and resume()
Section titled “pause() and resume()”music.pause();await DialogueState.show(game, 'Your Pokémon are fully healed!');music.resume();Pauses the current track where it is, then picks it back up from the same point.
setMuted(isMuted)
Section titled “setMuted(isMuted)”music.setMuted(true);Silences the music, or brings it back. Like pause(), unmuting picks the track back up where it was. A track asked for while muted starts when the music is unmuted. Muting only affects music: sound effects played through Sounds need their own check.
playJingle(name)
Section titled “playJingle(name)”await music.playJingle('level-up');Pauses the track, plays a short sound from Sounds once, then resumes the track. It returns a promise that resolves once the track is back, so await it to wait for the jingle to finish. It does nothing while muted.