Limit sound concurrency

This commit is contained in:
Caitlin Hennessy 2017-12-07 11:27:29 -08:00
parent 064d6f9411
commit 4e4aa266d5

View File

@ -256,6 +256,9 @@ namespace sounds {
} }
namespace music { namespace music {
let numSoundsPlaying = 0;
let soundsLimit = 3;
/** /**
* Plays a sound * Plays a sound
* @param sound the sound to play * @param sound the sound to play
@ -284,7 +287,8 @@ namespace music {
//% blockId=music_play_sound_effect block="play sound effect %sound" //% blockId=music_play_sound_effect block="play sound effect %sound"
//% weight=99 //% weight=99
export function playSoundEffect(sound: Sound) { export function playSoundEffect(sound: Sound) {
if (!sound) return; if (!sound || numSoundsPlaying >= soundsLimit) return;
control.runInBackground(() => sound.play()); numSoundsPlaying++;
control.runInBackground(() => {sound.play(); numSoundsPlaying--;});
} }
} }