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.
This commit is contained in:
Mike Welsh 2024-03-04 02:59:10 -08:00
parent 0a19c7a8cb
commit 5557dbf28f

View file

@ -90,6 +90,11 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
*/ */
var _label:String = "unknown"; var _label:String = "unknown";
/**
* Whether we received a focus lost event.
*/
var _lostFocus:Bool = false;
public function new() public function new()
{ {
super(); super();
@ -177,7 +182,10 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
*/ */
override function onFocus():Void 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(); resume();
} }
@ -185,6 +193,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
{ {
trace('Not resuming audio on focus!'); trace('Not resuming audio on focus!');
} }
_lostFocus = false;
} }
/** /**
@ -193,6 +202,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
override function onFocusLost():Void override function onFocusLost():Void
{ {
trace('Focus lost, pausing audio!'); trace('Focus lost, pausing audio!');
_lostFocus = true;
_alreadyPaused = _paused; _alreadyPaused = _paused;
pause(); pause();
} }