Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,28 @@ class FlxSound extends FlxBasic

/**
* Whether or not this sound should loop.
*
* **NOTE:** If `loopUntil` is 0 or more, than the sound will only loop until
* `loopCount` reaches `loopUntil`
*/
public var looped:Bool;

/**
* In case of looping, the point (in milliseconds) from where to restart the sound when it loops back
* The number of times this sound was restarted, via the `looped` flag.
* Automatically incremented on loops, and reset to 0 when restarted
* @since 6.2.0
*/
public var loopCount(default, null):Int = 0;

/**
* The number of times this sound should loop, where `-1` loops forever, and `1` is
* repeated once. This field is ignored if `looped` is `false`
* @since 6.2.0
*/
public var loopUntil:Int = -1;

/**
* The time (in milliseconds) from where to restart the sound when it loops back
* @since 4.1.0
*/
public var loopTime:Float = 0;
Expand Down Expand Up @@ -238,6 +255,8 @@ class FlxSound extends FlxBasic
_volumeAdjust = 1.0;
looped = false;
loopTime = 0.0;
loopCount = 0;
loopUntil = -1;
endTime = 0.0;
_target = null;
_radius = 0;
Expand Down Expand Up @@ -580,7 +599,10 @@ class FlxSound extends FlxBasic
if (_paused)
resume();
else
{
loopCount = 0;
startSound(StartTime);
}

endTime = EndTime;
return this;
Expand Down Expand Up @@ -742,14 +764,19 @@ class FlxSound extends FlxBasic
{
if (onComplete != null)
onComplete();
if (looped)

if (looped && (loopUntil == -1 || loopCount < loopUntil))
{
cleanup(false);
play(false, loopTime, endTime);
loopCount++;

cleanup(false, false);
startSound(loopTime);
}
else
cleanup(autoDestroy);
{
_time = 0; // Remove this line in 7.0.0
cleanup(autoDestroy, false);
}
}

/**
Expand Down Expand Up @@ -782,6 +809,7 @@ class FlxSound extends FlxBasic
{
_time = 0;
_paused = false;
loopCount = 0;
}
}

Expand Down
16 changes: 15 additions & 1 deletion tests/unit/src/flixel/sound/FlxSoundTest.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package flixel.sound;

class FlxSoundTest
import massive.munit.Assert;

class FlxSoundTest extends FlxTest
{
@Test // #1511
function testLoadInvalidSoundPathNoCrash()
Expand All @@ -9,4 +11,16 @@ class FlxSoundTest
sound.load("assets/invalid");
sound.play();
}

@Test
@Ignore("Unable to unit test sounds")
function testPlay()
{
var sound = new FlxSound();
sound.load("flxiel/sounds/flixel");
sound.play();
// step();

Assert.isTrue(sound.playing);
}
}