In my game, I want to play music sometimes. But music that is already playing shouldn’t be overridden.
So how can I
- Check if music is playing
- Manage that sound
Ideally in an global interface like this.sound
Thanks in advance
CodePudding user response:
After read your question I understand that you want to track the music.
The best solution would be like
- First play the music
- Store the id inside the a array
- Next time when you play the music check the id inside the array
- If the music id inside the array then play different music if not then play the music.
code example
let arr = [];
function playmusic(musicId){
if ((!arr .includes(musicId)) {
// your code
arr.push(musicId);
}
Take the idea from it and try.
CodePudding user response:
HTML5AudioSound
has isPlaying
member.
// inside scene method, create sound
const music = this.sound.add('music-name', {});
music.play();
// and later
if(music.isPlaying) {
}
See https://newdocs.phaser.io/docs/3.54.0/Phaser.Sound.HTML5AudioSound#isPlaying and https://newdocs.phaser.io/docs/3.52.0/Phaser.Sound.HTML5AudioSoundManager
(this.sound
is HTML5AudioSoundManager
instance)