From 4e4aa266d5347f2c5d35460251a8d856f9c6061b Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 7 Dec 2017 11:27:29 -0800 Subject: [PATCH] Limit sound concurrency --- libs/music/sounds.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index 42c9be73..44c7cef0 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -256,6 +256,9 @@ namespace sounds { } namespace music { + let numSoundsPlaying = 0; + let soundsLimit = 3; + /** * Plays a sound * @param sound the sound to play @@ -284,7 +287,8 @@ namespace music { //% blockId=music_play_sound_effect block="play sound effect %sound" //% weight=99 export function playSoundEffect(sound: Sound) { - if (!sound) return; - control.runInBackground(() => sound.play()); + if (!sound || numSoundsPlaying >= soundsLimit) return; + numSoundsPlaying++; + control.runInBackground(() => {sound.play(); numSoundsPlaying--;}); } }