-
Notifications
You must be signed in to change notification settings - Fork 459
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
Conversation
…tions for chaining
Is this sort of a Promise thing? I'm actually trying to implement those in my project right now. |
@larsiusprime Very nice! Chaining callbacks makes the code much more readable - does it change anything performance-wise? |
I think the two syntaxes I showed here will be about the same, but I haven't measured it. Also, tweens have never been particularly optimized with regards to performance -- it's using a big fat dynamic object to hold all your tweenable values, which it accesses with reflection, so even if this syntax added some weight it probably wouldn't be a big deal. But the whole point of tweens (as I understand it, I could be wrong) is an easy and simple way to animate, and it just has to be "fast enough" to not be the bottleneck. |
Yeah, i was actually hoping that somehow this could improve performance - even removing it from the call, the anonymous object still exists inside the tween right? |
Well, we should really profile it first to see if it's actually causing a bottleneck -- usually drawing, not update functions like this, are the culprit. Unless inefficient tweens are one of the top 10 causes of slowdown, or eating lots of memory, it's usually not worth optimizing it no matter how "bad" it looks. |
I know, it's not a bottleneck at all, at least here. If your entire game is made around FlxTweens it could matter though. Anyway i just wondered if there was any additional reason besides ease of use and readability for this change. 👍 |
Yeah, that makes sense! This is just a syntax upgrade -- it might have the slight benefit of not having to instantiate a new FlxTimer(), but I'm not sure that matters at all. If we did want to optimize out that dynamic object, I'm not sure it could be done without making the syntax a lot less convenient... I guess we'll just wait to see if anyone is using tweens for EVERYTHING 0_0 |
It looks like the Travis CI build that are failing is in RPG Interface, related to FlxUI, and only in legacy:
|
I don't really like how this is Interested in @JoeCreates' opinion on this since he seemed to have strong opinions on this in #1064. Also wondering if and how this affects his refactor concerning #1087. |
It could probably be refactored to not require VarTween |
@Gama11 Okay, did a quick refactor. What do you think now? |
|
||
typedef TweenParams = OneOfNine<VarTweenParams,AngleTweenParams,ColorTweenParams,NumTweenParams,CircularMotionParams,CubicMotionParams,LinearMotionParams,QuadMotionParams,QuadPathParams>; | ||
|
||
enum TweenType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling this "tween type" conflicts with FlxTween.PINGPONG
etc which are also considered tween types. It could see that causing quite a bit of confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative?
@Gama11 that better? |
Definitely less ambigious, yeah. |
|
||
enum TweenMethod | ||
{ | ||
None_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is TweenMethod.None
actually needed for anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, are the _
suffixes necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"None" is needed, yes, at least in this implementation. I use it for implementing the delays.
The suffixes are unfortunately necessary. I couldn't come up with a good naming convention otherwise:
attempt 1: remove "Tween"
AngleTween; //<---- these look fine
ColorTween;
NumTween;
VarTween;
CircularMotionTween; //<---- these conflict with their corresponding classes
CubicMotionTween;
LinearMotionTween;
QuadMotionTween;
QuadMotionTween;
attempt 2: just use one word
Angle; //<----these look fine
Color;
Num;
Var;
Circular; //<----Circular what? Cubic what?
Cubic;
Linear;
Quad;
Quad;
attempt 3: end with "tween"
AngleTween; //<---- these conflict with the classes now
ColorTween;
NumTween;
VarTween;
CircularMotionTween; //<---- these are okay
CubicMotionTween;
LinearMotionTween;
QuadMotionTween;
QuadMotionTween;
Do they conflict even if you use the fully qualified name? ( |
Maybe not, I haven't checked. Would it be confusing though? |
It does work fully-qualified. Is it okay if the names are otherwise the same as the classes? |
Not sure... This might not be the best approach to begin with. You're collecting the data needed to be able to act as a "tween factory" later when the tween whose turn it is is needed. That data is the "method" (kind of awkward naming here) and the data (dynamically typed). What about creating all the tweens in advance, which means FlxTween.tween(this, { y:endY }, time).
thenWait(DISPLAY_TIME).
thenTween(FlxTween.tween(this, { y:startY }, time, { onComplete: finishAnimation } )); |
Yeah, that could work I guess |
@Gama11 how's it look now? |
All those param typedefs etc can be removed now right? |
@Gama11 : yup. Just forgot. |
* @param params | ||
* @return | ||
*/ | ||
public function waitThen(Delay:Float, Tween:FlxTween):FlxTween |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there really much of a point to this combined function? It doesn't really add anything, and it's not really shorter either. Compare:
wait(1).then(tween)
waitThen(1, tween)
The first one seems more readable to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that's fine. One less function to maintain.
|
||
/** | ||
* After this tween has finished, do this tween next | ||
* @param method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well delete these empty @param
tags, especially considering they're out of sync with the method signature.
@@ -621,7 +718,7 @@ typedef TweenOptions = { | |||
?onUpdate:TweenCallback, | |||
?onComplete:TweenCallback, | |||
?startDelay:Null<Float>, | |||
?loopDelay:Null<Float>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, didn't know Haxe allowed a trailing comma here for the last element. :)
Good now? |
I've added some more comments, don't think you've addressed those yet? |
|
||
_thens.splice(0, 1); | ||
|
||
if (then.delay <= 0) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on it...
There was a problem hiding this comment.
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.
@Gama11 good now? Just tested it and it seems to work fine. Could probably use a demo eventually. |
@Gama11 Can we merge this today? I think I've addressed everything. |
FlxTween: add .wait(), and .then() for chaining, closes #1064
This lets you go from this:
To this: