diff --git a/flixel/sound/FlxSound.hx b/flixel/sound/FlxSound.hx index b74345ca5b..5af46a02a2 100644 --- a/flixel/sound/FlxSound.hx +++ b/flixel/sound/FlxSound.hx @@ -114,7 +114,8 @@ class FlxSound extends FlxBasic public var length(get, never):Float; /** - * The sound group this sound belongs to + * The sound group this sound belongs to, can only be in one group. + * NOTE: This setter is deprecated, use `group.add(sound)` or `group.remove(sound)`. */ public var group(default, set):FlxSoundGroup; @@ -252,6 +253,10 @@ class FlxSound extends FlxBasic override public function destroy():Void { + // Prevents double destroy + if (group != null) + group.remove(this); + _transform = null; exists = false; active = false; @@ -703,24 +708,20 @@ class FlxSound extends FlxBasic } #end - function set_group(group:FlxSoundGroup):FlxSoundGroup + @:deprecated("sound.group = myGroup is deprecated, use myGroup.add(sound)") // 5.7.0 + function set_group(value:FlxSoundGroup):FlxSoundGroup { - if (this.group != group) + if (value != null) { - var oldGroup:FlxSoundGroup = this.group; - - // New group must be set before removing sound to prevent infinite recursion - this.group = group; - - if (oldGroup != null) - oldGroup.remove(this); - - if (group != null) - group.add(this); - - updateTransform(); + // add to new group, also removes from prev and calls updateTransform + value.add(this); + } + else + { + // remove from prev group, also calls updateTransform + group.remove(this); } - return group; + return value; } inline function get_playing():Bool diff --git a/flixel/sound/FlxSoundGroup.hx b/flixel/sound/FlxSoundGroup.hx index c768beb338..cc37421248 100644 --- a/flixel/sound/FlxSoundGroup.hx +++ b/flixel/sound/FlxSoundGroup.hx @@ -25,16 +25,22 @@ class FlxSoundGroup } /** - * Add a sound to this group + * Add a sound to this group, will remove the sound from any group it is currently in * @param sound The sound to add to this group * @return True if sound was successfully added, false otherwise */ public function add(sound:FlxSound):Bool { - if (sounds.indexOf(sound) < 0) + if (!sounds.contains(sound)) { + // remove from prev group + if (sound.group != null) + sound.group.sounds.remove(sound); + sounds.push(sound); + @:bypassAccessor sound.group = this; + sound.updateTransform(); return true; } return false; @@ -47,10 +53,13 @@ class FlxSoundGroup */ public function remove(sound:FlxSound):Bool { - if (sounds.indexOf(sound) >= 0) + if (sounds.contains(sound)) { + @:bypassAccessor sound.group = null; - return sounds.remove(sound); + sounds.remove(sound); + sound.updateTransform(); + return true; } return false; } diff --git a/flixel/system/frontEnds/SoundFrontEnd.hx b/flixel/system/frontEnds/SoundFrontEnd.hx index 8aed9ea036..4abab4d04a 100644 --- a/flixel/system/frontEnds/SoundFrontEnd.hx +++ b/flixel/system/frontEnds/SoundFrontEnd.hx @@ -106,6 +106,9 @@ class SoundFrontEnd */ public function playMusic(embeddedMusic:FlxSoundAsset, volume = 1.0, looped = true, ?group:FlxSoundGroup):Void { + if (group == null) + group = defaultMusicGroup; + if (music == null) { music = new FlxSound(); @@ -114,11 +117,11 @@ class SoundFrontEnd { music.stop(); } - + music.loadEmbedded(embeddedMusic, looped); music.volume = volume; music.persist = true; - music.group = (group == null) ? defaultMusicGroup : group; + group.add(music); music.play(); } @@ -180,14 +183,15 @@ class SoundFrontEnd function loadHelper(sound:FlxSound, volume:Float, group:FlxSoundGroup, autoPlay = false):FlxSound { + if (group == null) + group = defaultSoundGroup; + sound.volume = volume; - + group.add(sound); + if (autoPlay) - { sound.play(); - } - - sound.group = (group == null) ? defaultSoundGroup : group; + return sound; } @@ -308,7 +312,7 @@ class SoundFrontEnd { if (music != null && (forceDestroy || !music.persist)) { - destroySound(music); + music.destroy(); music = null; } @@ -316,18 +320,11 @@ class SoundFrontEnd { if (sound != null && (forceDestroy || !sound.persist)) { - destroySound(sound); + sound.destroy(); } } } - function destroySound(sound:FlxSound):Void - { - defaultMusicGroup.remove(sound); - defaultSoundGroup.remove(sound); - sound.destroy(); - } - /** * Toggles muted, also activating the sound tray. */ diff --git a/tests/unit/src/flixel/sound/FlxSoundGroupTest.hx b/tests/unit/src/flixel/sound/FlxSoundGroupTest.hx index 17530f3619..a33efd884f 100644 --- a/tests/unit/src/flixel/sound/FlxSoundGroupTest.hx +++ b/tests/unit/src/flixel/sound/FlxSoundGroupTest.hx @@ -1,5 +1,6 @@ package flixel.sound; +import flixel.FlxG; import massive.munit.Assert; class FlxSoundGroupTest @@ -8,7 +9,23 @@ class FlxSoundGroupTest function testLengthAfterAdd() { var group = new FlxSoundGroup(); - group.add(new FlxSound()); + var sound = new FlxSound(); + group.add(sound); Assert.areEqual(1, group.sounds.length); + FlxG.sound.defaultSoundGroup.add(sound); + Assert.areEqual(0, group.sounds.length); + Assert.areEqual(1, FlxG.sound.defaultSoundGroup.sounds.length); + } + + @:haxe.warning("-WDeprecated") + function testSetter() + { + var group = new FlxSoundGroup(); + var sound = new FlxSound(); + sound.group = group; + Assert.areEqual(1, group.sounds.length); + sound.group = FlxG.sound.defaultSoundGroup; + Assert.areEqual(0, group.sounds.length); + Assert.areEqual(1, FlxG.sound.defaultSoundGroup.sounds.length); } }