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

"Complete all timers" and "complete all tweens" helper functions #1782

Merged
merged 19 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions flixel/tweens/FlxTween.hx
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,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 @@ -796,4 +799,14 @@ class FlxTweenManager extends FlxBasic
remove(_tweens[0]);
}
}
/**
* Immediately updates all tweens to their endings.
*/
public function completeAll():Void
{
var longestDuration:Float = 0;
for (tween in _tweens)
longestDuration = Math.max(longestDuration, tween.duration);
update(longestDuration);
}
}
14 changes: 12 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,14 @@ class FlxTimerManager extends FlxBasic
{
FlxArrayUtil.clearArray(_timers);
}
}

/**
* Calls the onComplete function of all timers, then removes them from the timer manager.
*/
public function completeAll():Void
{
for (timer in _timers)
timer.onComplete(timer);
Copy link
Member

Choose a reason for hiding this comment

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

This completely bypasses all the timer logic. The user might check properties of the timer on the callback (one of the reasons why it's passed as an argument), and they would be inconsistent with how they would look like on normal completion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know. I think the method I used for tweens could work here, eh?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure, looks similarly hacky.

Copy link
Member

Choose a reason for hiding this comment

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

(definitely better though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you be more specific? I thought they were dissimilar, otherwise my question of whether to use the other method made no sense... IMO they are dissimilar in that the tweens are update()d to the end, which updates all the properties properly (which addressed your concern about bypassing logic, I thought), unlike here where I just called the callback without bothering with any properties.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's a better approach.. a bit unusual, but probably safe. And it might really just feel unusual because it wasn't possible to do this sort of thing before too long ago (you didn't have per-object control of elapsed until that argument was added to update()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, fair enough.

Unfortunately, I have trouble getting it to work the other way:

    public function completeAll():Void
    {
        for (timer in _timers)
            if (timer.loops == 1)
                timer.update(timer.timeLeft);
    }

...this somehow only fires about half my timers, whereas the original approach was working fine for me, somehow.

Really bizarre stuff, because I add them all at once:

        for (i in 0...randomizedLetters.length)
            new FlxTimer().start(0.5 + i * 0.05, function(?ignored:FlxTimer) { ...

But then if I add trace statements:

    public function completeAll():Void
    {
        trace("start");
        for (timer in _timers){
            trace("loops", timer.loops, "time", timer.time);
            if (timer.loops == 1)
                timer.update(timer.timeLeft);
        }
    }

I get this output, then, having to press a key twice to activate it, which it does in two batches...and still doesn't complete all the timers in the end! :

FlxTimer.hx:282: start
FlxTimer.hx:284: loops,1,time,3.45
FlxTimer.hx:284: loops,1,time,3.4000000000000004
FlxTimer.hx:284: loops,1,time,3.35
FlxTimer.hx:284: loops,1,time,3.3000000000000003
FlxTimer.hx:284: loops,1,time,3.25
FlxTimer.hx:284: loops,1,time,3.2
FlxTimer.hx:284: loops,1,time,0.8
FlxTimer.hx:284: loops,1,time,0.8500000000000001
FlxTimer.hx:284: loops,1,time,0.9
FlxTimer.hx:284: loops,1,time,0.95
FlxTimer.hx:284: loops,1,time,1
FlxTimer.hx:284: loops,1,time,1.05
FlxTimer.hx:284: loops,1,time,1.1
FlxTimer.hx:284: loops,1,time,1.15
FlxTimer.hx:284: loops,1,time,1.2000000000000002
FlxTimer.hx:284: loops,1,time,1.25
FlxTimer.hx:284: loops,1,time,1.3
FlxTimer.hx:284: loops,1,time,1.35
FlxTimer.hx:284: loops,1,time,1.4
FlxTimer.hx:284: loops,1,time,1.4500000000000002
FlxTimer.hx:284: loops,1,time,1.5
FlxTimer.hx:284: loops,1,time,1.55
FlxTimer.hx:284: loops,1,time,1.6
FlxTimer.hx:284: loops,1,time,1.6500000000000001
FlxTimer.hx:284: loops,1,time,1.7000000000000002
FlxTimer.hx:284: loops,1,time,1.75
FlxTimer.hx:284: loops,1,time,1.8
FlxTimer.hx:284: loops,1,time,1.85
FlxTimer.hx:282: start
FlxTimer.hx:284: loops,1,time,3.1500000000000004
FlxTimer.hx:284: loops,1,time,3.1
FlxTimer.hx:284: loops,1,time,3.0500000000000003
FlxTimer.hx:284: loops,1,time,3
FlxTimer.hx:284: loops,1,time,2.95
FlxTimer.hx:284: loops,1,time,2.9000000000000004
FlxTimer.hx:284: loops,1,time,2.85
FlxTimer.hx:284: loops,1,time,2.8000000000000003
FlxTimer.hx:284: loops,1,time,2.75
FlxTimer.hx:284: loops,1,time,2.7
FlxTimer.hx:284: loops,1,time,2.65
FlxTimer.hx:284: loops,1,time,2.6
FlxTimer.hx:284: loops,1,time,2.5500000000000003

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I don't know what could be causing this. To me, it's still a very useful function as I have it currently in the PR, and it therefore could probably be of use to someone else (after all, there's that undocumented code in there to allow negative numbers of loops...surely that originated the same way ;) ) I propose keeping it, with a caveat, which I've just added, until such time as someone has time to widen its applicability.

Copy link
Member

Choose a reason for hiding this comment

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

You can't iterate on timers while also removing them from the timer array (which update()->cancel() does), if this is the issue here. It's currently an issue in github, but you would have to manually onComplete all the timers and then cancel all the timers in another loop.

It's like when you kill an enemy; you loop through all the enemies, push the dead ones into a temp array, and then loop through the temp array and remove those from the main one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! That makes sense. Thanks for explaining. Maybe a while loop could work here. Unless we want only non-loopers like in the tweens. Then a temp array is the only way to go. Will do.

clear();
}
}