Make sound effects pause with the game

This commit is contained in:
Hyper_
2025-06-10 17:23:46 -04:00
committed by Eric
parent 70f0a54191
commit d968fb271c
2 changed files with 39 additions and 5 deletions
+1 -1
View File
@@ -1026,9 +1026,9 @@ class PauseSubState extends MusicBeatSubState
#if FEATURE_MOBILE_ADVERTISEMENTS
AdMobUtil.removeBanner();
#end
state.close();
if (FlxG.sound.music != null) FlxG.sound.music.pause(); // Don't reset song position!
PlayState.instance.close(); // This only works because PlayState is a substate!
state.close();
}
}
+38 -4
View File
@@ -8,6 +8,7 @@ import flixel.FlxObject;
import flixel.FlxSubState;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
import flixel.sound.FlxSound;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
@@ -449,6 +450,11 @@ class PlayState extends MusicBeatSubState
*/
var cameraTweensPausedBySubState:List<FlxTween> = new List<FlxTween>();
/**
* Track any sounds we've paused for a Pause substate, so we can unpause them when we return.
*/
var soundsPausedBySubState:List<FlxSound> = new List<FlxSound>();
/**
* False until `create()` has completed.
*/
@@ -891,7 +897,6 @@ class PlayState extends MusicBeatSubState
super.update(elapsed);
var list = FlxG.sound.list;
updateHealthBar();
updateScoreText();
@@ -1361,9 +1366,25 @@ class PlayState extends MusicBeatSubState
musicPausedBySubState = true;
}
// Pause vocals.
// Not tracking that we've done this via a bool because vocal re-syncing involves pausing the vocals anyway.
if (vocals != null) vocals.pause();
// Pause any sounds that are playing and keep track of them.
// Vocals are also paused here but are not included as they are handled separately.
if (Std.isOfType(subState, PauseSubState))
{
FlxG.sound.list.forEachAlive(function(sound:FlxSound) {
// The need to check for time is obligatory because of scheduled sounds
if (!sound.active || sound == FlxG.sound.music || (!sound.playing && sound.time >= 0)) return;
sound.pause();
soundsPausedBySubState.add(sound);
});
vocals?.forEach(function(voice:FunkinSound) {
soundsPausedBySubState.remove(voice);
});
}
else
{
vocals?.pause();
}
}
// Pause camera tweening, and keep track of which tweens we pause.
@@ -1419,6 +1440,8 @@ class PlayState extends MusicBeatSubState
musicPausedBySubState = false;
}
forEachPausedSound((s) -> needsReset ? s.destroy() : s.resume());
// Resume camera tweens if we paused any.
for (camTween in cameraTweensPausedBySubState)
{
@@ -3423,6 +3446,8 @@ class PlayState extends MusicBeatSubState
}
}
forEachPausedSound((s) -> s.destroy());
// Remove reference to stage and remove sprites from it to save memory.
if (currentStage != null)
{
@@ -3731,6 +3756,15 @@ class PlayState extends MusicBeatSubState
scrollSpeedTweens = [];
}
function forEachPausedSound(f:FlxSound->Void):Void
{
for (sound in soundsPausedBySubState)
{
f(sound);
}
soundsPausedBySubState.clear();
}
#if FEATURE_DEBUG_FUNCTIONS
/**
* Jumps forward or backward a number of sections in the song.