A simple timer class, calls the given function after the specified amount of time passed. FlxTimers are automatically updated and managed by the globalManager. They are deterministic by default, unless FlxG.fixedTimestep is set to false.

Note: timer duration is affected when FlxG.timeScale is changed.

Example: to create a timer that executes a function in 3 seconds

new FlxTimer().start(3.0, ()->{ FlxG.log.add("The FlxTimer has finished"); })

See also:

Static variables

staticglobalManager:FlxTimerManager

The global timer manager that handles global timers

Available since

4.2.0

.

Static methods

staticinlineloop(time:Float, onComplete:(loop:Int) ‑> Void, loops:Int):FlxTimer

Handy tool to create and start a FlxTimer

Parameters:

time

The duration of the timer, in seconds. If 0 then onComplete fires on the next game update, and the loops argument is ignored.

onComplete

triggered whenever the time runs out, once for each loop. Should take a single Int arg representing the number of completed loops

loops

How many times the timer should go off. 0 means "looping forever".

Returns:

The FlxTimer instance

staticinlinewait(time:Float, onComplete:() ‑> Void):FlxTimer

Handy tool to create and start a FlxTimer

Parameters:

time

The duration of the timer, in seconds. If 0 then onComplete fires on the next game update.

onComplete

Triggered whenever the time runs out

Returns:

The FlxTimer instance

Constructor

new(?manager:FlxTimerManager)

Creates a new timer.

Variables

@:value(false)active:Bool = false

Pauses or checks the pause state of the timer.

read onlyelapsedLoops:Int

Read-only: how many loops that have elapsed since the timer was started.

read onlyelapsedTime:Float

Read-only: The amount of milliseconds that have elapsed since the timer was started

@:value(false)finished:Bool = false

Check to see if the timer is finished.

@:value(0)loops:Int = 0

How many loops the timer was set for. 0 means "looping forever".

read onlyloopsLeft:Int

Read-only: check how many loops are left on the timer.

manager:FlxTimerManager

The manager to which this timer belongs

Available since

4.2.0

.

read onlyprogress:Float

Read-only: how far along the timer is, on a scale of 0.0 to 1.0.

@:value(0)time:Float = 0

How much time the timer was set for.

read onlytimeLeft:Float

Read-only: check how much time is left on the timer.

onComplete:FlxTimer ‑> Void

Called when timer completes. The function header should be (timer:FlxTimer)

Methods

cancel():Void

Stops the timer and removes it from the timer manager.

destroy():Void

Clean up memory.

@:value({ newTime : -1 })reset(newTime:Float = -1):FlxTimer

Restart the timer using the new duration

Parameters:

newTime

The duration of this timer in seconds.

@:value({ loops : 1, time : 1 })start(time:Float = 1, ?onComplete:FlxTimer ‑> Void, loops:Int = 1):FlxTimer

Starts the timer and adds the timer to the timer manager.

Parameters:

time

The duration of the timer, in seconds. If 0 then onComplete fires on the next game update, and the loops argument is ignored.

onComplete

Optional, triggered whenever the time runs out, once for each loop. The function header should be (timer:FlxTimer)

loops

How many times the timer should go off. 0 means "looping forever".

Returns:

A reference to itself (handy for chaining or whatever).

update(elapsed:Float):Void

Called by the timer manager plugin to update the timer. If time runs out, the loop counter is advanced, the timer reset, and the callback called if it exists. If the timer runs out of loops, then the timer calls cancel(). However, callbacks are called AFTER cancel() is called.