From 5557dbf28fe490419e0db12bbc25982ff9bddc1f Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Mon, 4 Mar 2024 02:59:10 -0800 Subject: [PATCH] Avoid unpausing music when debugger is toggled Workaround a Flixel issue where sounds can unpause after toggling the Flixel debugger with F2, caused by the debugger throwing a spurious onFocus event without an onFocusLost event. --- source/funkin/audio/FunkinSound.hx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/funkin/audio/FunkinSound.hx b/source/funkin/audio/FunkinSound.hx index 06ba48e81..cf28ed08e 100644 --- a/source/funkin/audio/FunkinSound.hx +++ b/source/funkin/audio/FunkinSound.hx @@ -90,6 +90,11 @@ class FunkinSound extends FlxSound implements ICloneable */ var _label:String = "unknown"; + /** + * Whether we received a focus lost event. + */ + var _lostFocus:Bool = false; + public function new() { super(); @@ -177,7 +182,10 @@ class FunkinSound extends FlxSound implements ICloneable */ override function onFocus():Void { - if (!_alreadyPaused) + // Flixel can sometimes toss spurious `onFocus` events, e.g. if the Flixel debugger is toggled + // on and off. We only want to resume the sound if we actually lost focus, and if we weren't + // already paused before we lost focus. + if (_lostFocus && !_alreadyPaused) { resume(); } @@ -185,6 +193,7 @@ class FunkinSound extends FlxSound implements ICloneable { trace('Not resuming audio on focus!'); } + _lostFocus = false; } /** @@ -193,6 +202,7 @@ class FunkinSound extends FlxSound implements ICloneable override function onFocusLost():Void { trace('Focus lost, pausing audio!'); + _lostFocus = true; _alreadyPaused = _paused; pause(); }