Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlxTween: add .wait(), and .then() for chaining, closes #1064 #1614

Merged
merged 14 commits into from
Oct 4, 2015
109 changes: 102 additions & 7 deletions flixel/tweens/FlxTween.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.tweens.FlxEase.EaseFunction;
import flixel.tweens.FlxTween.TweenOptions;
import flixel.tweens.misc.AngleTween;
import flixel.tweens.misc.ColorTween;
import flixel.tweens.misc.NumTween;
Expand Down Expand Up @@ -373,7 +374,8 @@ class FlxTween implements IFlxDestroyable
private var _delayToUse:Float = 0;
private var _running:Bool = false;
private var _waitingForRestart:Bool = false;

private var _thens:Array<ChainedTween>;

/**
* This function is called when tween is created, or recycled.
*/
Expand Down Expand Up @@ -406,8 +408,41 @@ class FlxTween implements IFlxDestroyable
onUpdate = null;
onComplete = null;
ease = null;
_thens = null;
}


/**
* After this tween has finished, do this tween next
* @param Tween The FlxTween to run next
* @return
*/

public function then(Tween:FlxTween):FlxTween
{
Tween.cancelSoft();
if (_thens == null)
{
_thens = [];
}
_thens.push(new ChainedTween(0, Tween));
return this;
}

/**
* After this tween has finished, wait this many seconds, then do the next chained "then" command
* @param Delay The number of seconds to wait
* @return
*/
public function wait(Delay:Float):FlxTween
{
if (_thens == null)
{
_thens = [];
}
_thens.push(new ChainedTween(Delay, null));
return this;
}

private function update(elapsed:Float):Void
{
_secondsSinceStart += elapsed;
Expand Down Expand Up @@ -472,6 +507,14 @@ class FlxTween implements IFlxDestroyable
manager.remove(this);
}

private function cancelSoft():Void
{
active = false;
_running = false;
finished = true;
manager.remove(this, false);
}

private function finish():Void
{
executions++;
Expand Down Expand Up @@ -523,6 +566,43 @@ class FlxTween implements IFlxDestroyable
active = false;
_running = false;
finished = true;

if (_thens != null && _thens.length > 0)
{
var then = _thens[0];

_thens.splice(0, 1);

if (then.delay <= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it might fail with wait(0) (which should just be "no wait" - doesn't make sense if written like that but the delay could be dynamically set from some variable that happens to become 0 at some point)? And what's the expected result for negative values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you were, it needs to call onEnd() again in the case that delay is <= 0 AND the tween is null, signifying it's a delay that should terminate instantly and run the next "then" command.

{
if (then.tween != null)
{
doNextTween(then.tween, _thens);
}
else
{
onEnd();
}
}
else
{
doNextTween(FlxTween.num(0,0,then.delay), _thens);
}
_thens = null;
}
}

private function doNextTween(tween:FlxTween, thens:Array<ChainedTween>):Void
{
if (!tween.active)
{
tween.start();
manager.add(tween);
}
if (thens != null)
{
tween._thens = _thens;
}
}

/**
Expand Down Expand Up @@ -621,7 +701,7 @@ typedef TweenOptions = {
?onUpdate:TweenCallback,
?onComplete:TweenCallback,
?startDelay:Null<Float>,
?loopDelay:Null<Float>,
?loopDelay:Null<Float>
}

@:access(flixel.tweens.FlxTween)
Expand Down Expand Up @@ -705,25 +785,28 @@ class FlxTweenManager extends FlxBasic
* Remove a FlxTween.
*
* @param Tween The FlxTween to remove.
* @param Destroy Whether you want to destroy the FlxTween.
* @param Destroy Whether you want to destroy the FlxTween
* @return The added FlxTween object.
*/
@:allow(flixel.tweens.FlxTween)
private function remove(Tween:FlxTween):FlxTween
private function remove(Tween:FlxTween, Destroy:Bool=true):FlxTween
{
if (Tween == null)
{
return null;
}

Tween.active = false;
Tween.destroy();

if (Destroy)
{
Tween.destroy();
}

FlxArrayUtil.fastSplice(_tweens, Tween);

return Tween;
}

/**
* Removes all FlxTweens.
*/
Expand All @@ -735,3 +818,15 @@ class FlxTweenManager extends FlxBasic
}
}
}

private class ChainedTween
{
public var delay:Float;
public var tween:FlxTween;

public function new(Delay:Float, Tween:FlxTween)
{
delay = Delay;
tween = Tween;
}
}