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

Add onFinishEnd to FlxAnimation and FlxAnimationController #9

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
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
58 changes: 53 additions & 5 deletions flixel/animation/FlxAnimation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,28 @@ class FlxAnimation extends FlxBaseAnimation
public var frames:Array<Int>;

/**
* If addByIndicies was used
* If addByIndices was used
*/
public var usesIndicies:Bool = false;
public var usesIndices:Bool = false;

@:noCompletion public var usesIndicies(get, set):Bool;

inline function get_usesIndicies():Bool
return usesIndices;

inline function set_usesIndicies(value:Bool):Bool
return usesIndices = value;

/**
* Internal, used to time each frame of animation.
*/
var _frameTimer:Float = 0;

/**
* Internal, used to wait the frameDuration at the end of the animation.
*/
var _frameFinishedEndTimer:Float = 0;

/**
* How fast or slow time should pass for this animation.
*
Expand All @@ -106,6 +119,7 @@ class FlxAnimation extends FlxBaseAnimation
public var timeScale:Float = 1.0;

public var onFinish:FlxTypedSignal<Void->Void> = new FlxTypedSignal();
public var onFinishEnd:FlxTypedSignal<Void->Void> = new FlxTypedSignal();
public var onPlay:FlxTypedSignal<String->Bool->Bool->Int->Void> = new FlxTypedSignal();
public var onLoop:FlxTypedSignal<Void->Void> = new FlxTypedSignal();

Expand Down Expand Up @@ -134,7 +148,9 @@ class FlxAnimation extends FlxBaseAnimation
override public function destroy():Void
{
FlxDestroyUtil.destroy(onFinish);
FlxDestroyUtil.destroy(onFinishEnd);
FlxDestroyUtil.destroy(onPlay);
FlxDestroyUtil.destroy(onLoop);
frames = null;
name = null;
super.destroy();
Expand Down Expand Up @@ -177,9 +193,19 @@ class FlxAnimation extends FlxBaseAnimation
}

if (finished)
parent.fireFinishCallback(name);
{
_frameFinishedEndTimer = frameDuration;
onFinish.dispatch();
if (parent != null)
parent.fireFinishCallback(name);
}
else
{
_frameFinishedEndTimer = 0;
}

parent.firePlayCallback(name, Force, Reversed, curFrame);
if (parent != null)
parent.firePlayCallback(name, Force, Reversed, curFrame);
onPlay.dispatch(name, Force, Reversed, curFrame);
}

Expand Down Expand Up @@ -223,10 +249,31 @@ class FlxAnimation extends FlxBaseAnimation
play(false, reversed);
}

inline function _doFinishedEndCallback():Void
{
parent.onFinishEnd.dispatch(name);
onFinishEnd.dispatch();
}

override public function update(elapsed:Float):Void
{
if (paused)
return;

if (_frameFinishedEndTimer > 0)
{
_frameFinishedEndTimer -= elapsed * timeScale;
if (_frameFinishedEndTimer <= 0)
{
_frameFinishedEndTimer = 0;
_doFinishedEndCallback();
}
}
if (finished)
return;

var curFrameDuration = getCurrentFrameDuration();
if (curFrameDuration == 0 || finished || paused)
if (curFrameDuration == 0)
return;

_frameTimer += elapsed * timeScale;
Expand Down Expand Up @@ -306,6 +353,7 @@ class FlxAnimation extends FlxBaseAnimation

if (finished)
{
_frameFinishedEndTimer = frameDuration;
onFinish.dispatch();
if (parent != null)
parent.fireFinishCallback(name);
Expand Down
8 changes: 8 additions & 0 deletions flixel/animation/FlxAnimationController.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class FlxAnimationController implements IFlxDestroyable
*/
public final onFinish = new FlxTypedSignal<(animName:String) -> Void>();

/**
* Dispatches each time the last frame of an animation finishes.
*
* @param animName The name of the animation that ended
*/
public final onFinishEnd = new FlxTypedSignal<(animName:String) -> Void>();

/**
* Dispatches each time the current animation is played.
*
Expand Down Expand Up @@ -201,6 +208,7 @@ class FlxAnimationController implements IFlxDestroyable
{
FlxDestroyUtil.destroy(onFrameChange);
FlxDestroyUtil.destroy(onFinish);
FlxDestroyUtil.destroy(onFinishEnd);
FlxDestroyUtil.destroy(onLoop);

destroyAnimations();
Expand Down