Skip to content
Download

Timers

Timers run a function later. They count in steps: 60 steps = 1 second.

Function What it does Example
TimerId after(int steps, std::function<void()> fn) Calls fn once, steps steps from now (at least 1). after(30, [this] { visible = true; });
TimerId every(int steps, std::function<void()> fn) Calls fn every steps steps, until it is cancelled or the instance is destroyed. every(60, [this] { spawn<Bullet>(x, y, "Instancias"); });
TimerId afterSeconds(float seconds, std::function<void()> fn) Same as after, in seconds. afterSeconds(2.5f, [this] { destroy(); });
void cancel(TimerId id) Cancels a timer of this instance. It works even inside the timer itself. cancel(blink_);
TimerId tween(float from, float to, float seconds, std::function<void(float)> apply, Ease ease = Ease::Linear) Takes a value from from to to in seconds seconds, calling apply on every step. Ease::Out slows down at the end; there are also In, InOut and Back. tween(y, y - 20, 0.3f, [this](float v) { y = v; }, Ease::Out);
  • TimerId identifies the timer. An empty TimerId has value == 0.
  • The timers run at the beginning of the step, before onUpdate.
  • When the instance is destroyed, its timers go away with it.
  • The [this] in the lambda lets you use the instance’s variables and functions. See Lambdas.

See Wait and repeat for examples.