From 5b3aa8c423727622bc97bd7f9112df7a34ca1d2b Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sun, 18 Feb 2024 18:08:56 -0600 Subject: [PATCH 1/5] add FlxTimer.wait --- flixel/util/FlxTimer.hx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/flixel/util/FlxTimer.hx b/flixel/util/FlxTimer.hx index a7a4347d33..4f66b21eeb 100644 --- a/flixel/util/FlxTimer.hx +++ b/flixel/util/FlxTimer.hx @@ -21,6 +21,20 @@ import flixel.util.FlxDestroyUtil.IFlxDestroyable; */ class FlxTimer implements IFlxDestroyable { + /** + * Handy tool to create and start a `FlxTimer` + * @param time The duration of the timer, in seconds. If `0` then `onComplete` + * fires on the next game update, and the `loops` argument is ignored. + * @param onComplete Optional, triggered whenever the time runs out, once for each loop. + * Callback should be formed `onTimer(Timer:FlxTimer);` + * @param loops How many times the timer should go off. `0` means "looping forever". + * @return The `FlxTimer` instance + */ + public static inline function wait(time:Float, callback:(FlxTimer)->Void, loops = 1) + { + return new FlxTimer().start(time, onComplete, loops); + } + /** * The global timer manager that handles global timers * @since 4.2.0 @@ -115,14 +129,14 @@ class FlxTimer implements IFlxDestroyable /** * Starts the timer and adds the timer to the timer manager. * - * @param time How many seconds it takes for the timer to go off. - * If 0 then timer will fire OnComplete callback only once at the first call of update method (which means that Loops argument will be ignored). + * @param time The duration of the timer, in seconds. If `0` then `onComplete` + * fires on the next game update, and the `loops` argument is ignored. * @param onComplete Optional, triggered whenever the time runs out, once for each loop. - * Callback should be formed "onTimer(Timer:FlxTimer);" + * Callback should be formed `onTimer(Timer:FlxTimer);` * @param loops How many times the timer should go off. 0 means "looping forever". * @return A reference to itself (handy for chaining or whatever). */ - public function start(time:Float = 1, ?onComplete:FlxTimer->Void, loops:Int = 1):FlxTimer + public function start(time:Float = 1, ?onComplete:(FlxTimer)->Void, loops:Int = 1):FlxTimer { if (manager != null && !_inManager) { From 425daef973b23744e666a25cb7baedbf11d9d87a Mon Sep 17 00:00:00 2001 From: George FunBook Date: Wed, 21 Feb 2024 14:34:42 -0600 Subject: [PATCH 2/5] fix arg --- flixel/util/FlxTimer.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/util/FlxTimer.hx b/flixel/util/FlxTimer.hx index 4f66b21eeb..45081f1563 100644 --- a/flixel/util/FlxTimer.hx +++ b/flixel/util/FlxTimer.hx @@ -30,7 +30,7 @@ class FlxTimer implements IFlxDestroyable * @param loops How many times the timer should go off. `0` means "looping forever". * @return The `FlxTimer` instance */ - public static inline function wait(time:Float, callback:(FlxTimer)->Void, loops = 1) + public static inline function wait(time:Float, onComplete:(FlxTimer)->Void, loops = 1) { return new FlxTimer().start(time, onComplete, loops); } From 0d231005f599fc546b1ceff90e5f54e6855e01cc Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 23 Feb 2024 12:45:09 -0600 Subject: [PATCH 3/5] remove loops from wait, take no arg in callback, add loop() --- flixel/util/FlxTimer.hx | 20 ++++++++++++---- tests/unit/src/flixel/util/FlxTimerTest.hx | 27 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/flixel/util/FlxTimer.hx b/flixel/util/FlxTimer.hx index 45081f1563..36ece73753 100644 --- a/flixel/util/FlxTimer.hx +++ b/flixel/util/FlxTimer.hx @@ -21,18 +21,30 @@ import flixel.util.FlxDestroyUtil.IFlxDestroyable; */ class FlxTimer implements IFlxDestroyable { + /** + * Handy tool to create and start a `FlxTimer` + * @param time The duration of the timer, in seconds. If `0` then `onComplete` + * fires on the next game update. + * @param onComplete Triggered whenever the time runs out + * @return The `FlxTimer` instance + */ + public static inline function wait(time:Float, onComplete:()->Void) + { + return new FlxTimer().start(time, (_)->onComplete()); + } + /** * Handy tool to create and start a `FlxTimer` * @param time The duration of the timer, in seconds. If `0` then `onComplete` * fires on the next game update, and the `loops` argument is ignored. - * @param onComplete Optional, triggered whenever the time runs out, once for each loop. - * Callback should be formed `onTimer(Timer:FlxTimer);` + * @param onComplete triggered whenever the time runs out, once for each loop. + * Should take a single `Int` arg representing the number of completed loops * @param loops How many times the timer should go off. `0` means "looping forever". * @return The `FlxTimer` instance */ - public static inline function wait(time:Float, onComplete:(FlxTimer)->Void, loops = 1) + public static inline function loop(time:Float, onComplete:(loop:Int)->Void, loops:Int) { - return new FlxTimer().start(time, onComplete, loops); + return new FlxTimer().start(time, (t)->onComplete(loops - t.loopsLeft), loops); } /** diff --git a/tests/unit/src/flixel/util/FlxTimerTest.hx b/tests/unit/src/flixel/util/FlxTimerTest.hx index ab10d6495f..edc3519b2d 100644 --- a/tests/unit/src/flixel/util/FlxTimerTest.hx +++ b/tests/unit/src/flixel/util/FlxTimerTest.hx @@ -18,8 +18,9 @@ class FlxTimerTest extends FlxTest { var calledBack:Bool = false; timer.start(0, function(_) calledBack = true); + + Assert.isFalse(calledBack); step(); - Assert.isTrue(calledBack); } @@ -67,4 +68,28 @@ class FlxTimerTest extends FlxTest Assert.isTrue(timer2.progress > 0); Assert.isTrue(timer3.progress > 0); } + + @Test + function testWait() + { + var calledBack = false; + function onComplete() { calledBack = true; } + final timer1 = FlxTimer.wait(2/60, onComplete); + final timer2 = FlxTimer.wait(0.0001, timer1.cancel); + + step(3); + Assert.isTrue(timer1.finished && calledBack); + } + + @Test + function testLoop() + { + var calledBack = false; + function onComplete(n) { calledBack = true; } + final timer1 = FlxTimer.loop(2/60, onComplete, 0); + final timer2 = FlxTimer.loop(0.0001, (loop)->{ if (loop == 3) timer1.cancel(); }, 3); + + step(3); + Assert.isTrue(timer1.finished && calledBack); + } } From 083462c78fa277958339afd2fcefbb17b7321e1c Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 23 Feb 2024 13:54:46 -0600 Subject: [PATCH 4/5] simplify wait --- flixel/util/FlxTimer.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/util/FlxTimer.hx b/flixel/util/FlxTimer.hx index 36ece73753..778e49fed1 100644 --- a/flixel/util/FlxTimer.hx +++ b/flixel/util/FlxTimer.hx @@ -44,7 +44,7 @@ class FlxTimer implements IFlxDestroyable */ public static inline function loop(time:Float, onComplete:(loop:Int)->Void, loops:Int) { - return new FlxTimer().start(time, (t)->onComplete(loops - t.loopsLeft), loops); + return new FlxTimer().start(time, (t)->onComplete(t.elapsedLoops), loops); } /** From 4f16cf7de182410ef652ef8f09985ad88b95d6eb Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 23 Feb 2024 20:56:17 -0600 Subject: [PATCH 5/5] docs --- flixel/util/FlxTimer.hx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flixel/util/FlxTimer.hx b/flixel/util/FlxTimer.hx index 778e49fed1..4495fc7d3a 100644 --- a/flixel/util/FlxTimer.hx +++ b/flixel/util/FlxTimer.hx @@ -80,10 +80,9 @@ class FlxTimer implements IFlxDestroyable public var finished:Bool = false; /** - * Function that gets called when timer completes. - * Callback should be formed "onTimer(Timer:FlxTimer);" + * Called when timer completes. The function header should be `(timer:FlxTimer)` */ - public var onComplete:FlxTimer->Void; + public var onComplete:(FlxTimer)->Void; /** * Read-only: check how much time is left on the timer. @@ -144,7 +143,7 @@ class FlxTimer implements IFlxDestroyable * @param time The duration of the timer, in seconds. If `0` then `onComplete` * fires on the next game update, and the `loops` argument is ignored. * @param onComplete Optional, triggered whenever the time runs out, once for each loop. - * Callback should be formed `onTimer(Timer:FlxTimer);` + * The function header should be `(timer:FlxTimer)` * @param loops How many times the timer should go off. 0 means "looping forever". * @return A reference to itself (handy for chaining or whatever). */