Implementing Timer and its examples

This commit is contained in:
erickzanardo
2019-06-25 12:50:44 -03:00
committed by Erick
parent 27cb0cfff5
commit 4b2288623e
6 changed files with 167 additions and 5 deletions

View File

@ -1,19 +1,36 @@
/// Simple utility class that helps handling time counting and implementing interval like events.
///
class Timer {
final double _limit;
void Function() _callback;
double _current;
bool _repeat;
double _current = 0;
bool _running = false;
Timer(this._limit, this._callback);
Timer(this._limit, { repeat = false, callback }) {
_repeat = repeat;
_callback = callback;
}
double get current {
return _current;
}
void update(double dt) {
if (_running) {
_current += dt;
if (isFinished()) {
_running = false;
_callback();
if (_repeat) {
_current -= _limit;
} else {
_running = false;
}
if (_callback != null) {
_callback();
}
}
}
}
@ -26,7 +43,7 @@ class Timer {
_current = 0;
_running = true;
}
void stop() {
_current = 0;
_running = false;