diff --git a/flixel/sound/FlxSound.hx b/flixel/sound/FlxSound.hx index 65fa88c922..4047bae491 100644 --- a/flixel/sound/FlxSound.hx +++ b/flixel/sound/FlxSound.hx @@ -114,9 +114,16 @@ 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; + public var group(get, set):FlxSoundGroup; + + /** + * The sound group this sound belongs to. This is a temporary proxy for group, until it is removed + */ + @:allow(flixel.sound.FlxSoundGroup) + var _group:FlxSoundGroup; /** * Whether or not this sound should loop. @@ -707,24 +714,31 @@ class FlxSound extends FlxBasic } #end - function set_group(group:FlxSoundGroup):FlxSoundGroup + // Will be removed in a major version, and become a simple `(default,null)` var + inline function get_group():FlxSoundGroup { - if (this.group != group) + return _group; + } + + @:deprecated("sound.group = myGroup is deprecated, use myGroup.add(sound)") // 5.7.0 + function set_group(value:FlxSoundGroup):FlxSoundGroup + { + if (_group != value) { - var oldGroup:FlxSoundGroup = this.group; + final oldGroup = _group; // New group must be set before removing sound to prevent infinite recursion - this.group = group; + _group = value; if (oldGroup != null) oldGroup.remove(this); - if (group != null) - group.add(this); - + if (value != null) + value.add(this); + updateTransform(); } - return group; + return value; } inline function get_playing():Bool diff --git a/flixel/sound/FlxSoundGroup.hx b/flixel/sound/FlxSoundGroup.hx index c768beb338..0f7e4222bb 100644 --- a/flixel/sound/FlxSoundGroup.hx +++ b/flixel/sound/FlxSoundGroup.hx @@ -25,16 +25,20 @@ 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)) { + if (sound._group != null) + sound._group.sounds.remove(sound); + sounds.push(sound); - sound.group = this; + sound._group = this; + sound.updateTransform(); return true; } return false; @@ -47,10 +51,12 @@ class FlxSoundGroup */ public function remove(sound:FlxSound):Bool { - if (sounds.indexOf(sound) >= 0) + if (sounds.contains(sound)) { - sound.group = null; - return sounds.remove(sound); + sound._group = null; + sounds.remove(sound); + sound.updateTransform(); + return true; } return false; } diff --git a/flixel/system/frontEnds/SoundFrontEnd.hx b/flixel/system/frontEnds/SoundFrontEnd.hx index 32aaf57a1d..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; }