Skip to content

Commit

Permalink
"Complete all timers/tweens" and "forEach" helper functions (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
IBwWG authored and Gama11 committed Aug 21, 2016
1 parent 00c0e20 commit dfc8470
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
30 changes: 30 additions & 0 deletions flixel/tweens/FlxTween.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import flixel.tweens.motion.QuadPath;
import flixel.util.FlxArrayUtil;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;

class FlxTween implements IFlxDestroyable
Expand Down Expand Up @@ -729,6 +730,9 @@ typedef TweenOptions =
?loopDelay:Null<Float>
}

/**
* A simple manager for tracking and updating game tween objects. Normally accessed via the static `FlxTween.manager` rather than being created separately.
*/
@:access(flixel.tweens.FlxTween)
class FlxTweenManager extends FlxBasic
{
Expand Down Expand Up @@ -830,4 +834,30 @@ class FlxTweenManager extends FlxBasic
while (_tweens.length > 0)
remove(_tweens[0]);
}
/**
* Immediately updates all tweens of type PERSIST or ONESHOT through their endings, triggering their onComplete callbacks.
*
* Note: if they haven't yet begun, this will first trigger their onStart callback.
*
* Note: their onComplete callbacks are triggered in the next frame. To trigger them immediately, call `FlxTween.manager.update(0);` after this function.
*
* In no case should it trigger an onUpdate callback.
*/
public function completeAll():Void
{
for (tween in _tweens)
if (tween.type == FlxTween.PERSIST || tween.type == FlxTween.ONESHOT) {
tween.update(FlxMath.MAX_VALUE_FLOAT);
}
}
/**
* Applies a function to all tweens
*
* @param Function A function that modifies one tween at a time
*/
public function forEach(Function:FlxTween->Void)
{
for (tween in _tweens)
Function(tween);
}
}
27 changes: 25 additions & 2 deletions flixel/util/FlxTimer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class FlxTimer implements IFlxDestroyable
}

/**
* A simple manager for tracking and updating game timer objects.
* A simple manager for tracking and updating game timer objects. Normally accessed via the static `FlxTimer.manager` rather than being created separately.
*/
class FlxTimerManager extends FlxBasic
{
Expand Down Expand Up @@ -273,4 +273,27 @@ class FlxTimerManager extends FlxBasic
{
FlxArrayUtil.clearArray(_timers);
}
}

/**
* Immediately updates all non-looping timers to their end points.
*/
public function completeAll():Void
{
var timersToFinish:Array<FlxTimer> = [];
for (timer in _timers)
if (timer.loops == 1)
timersToFinish.push(timer);
for (timer in timersToFinish)
timer.update(timer.timeLeft);
}
/**
* Applies a function to all timers
*
* @param Function A function that modifies one timer at a time
*/
public function forEach(Function:FlxTimer->Void)
{
for (timer in _timers)
Function(timer);
}
}

0 comments on commit dfc8470

Please sign in to comment.