Files
flame/examples/lib/stories/utils/timer_example.dart
Lukas Klingsbo 8b132d7c0b Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified

* Added descriptions to effects

* Rename input games

* Unify input stories

* Add info to parallax section

* Added descriptions to the rendering examples

* Add descriptions to the sprites directory

* Fix utils and rendering section

* Add descriptions to the widgets section

* Delete directory that rebase brought back

* Unify game names

* Added some styleguide docs for examples

* Fix analyze issues

* All files should have _example as suffix

* Made the FollowComponentExample a bit easier to understand

* Change priority of ember
2021-11-19 14:28:04 +01:00

56 lines
1.3 KiB
Dart

import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/timer.dart';
import 'package:flutter/material.dart';
class TimerExample extends FlameGame with TapDetector {
static const String description = '''
This example shows how to use the `Timer`.\n\n
Tap down to start the countdown timer, it will then count to 5 and then stop
until you tap the canvas again and it restarts.
''';
final TextPaint textConfig = TextPaint(
style: const TextStyle(color: Colors.white, fontSize: 20),
);
late Timer countdown;
late Timer interval;
int elapsedSecs = 0;
@override
Future<void> onLoad() async {
await super.onLoad();
countdown = Timer(5);
interval = Timer(
1,
onTick: () => elapsedSecs += 1,
repeat: true,
);
interval.start();
}
@override
void onTapDown(_) {
countdown.start();
}
@override
void update(double dt) {
super.update(dt);
countdown.update(dt);
interval.update(dt);
}
@override
void render(Canvas canvas) {
super.render(canvas);
textConfig.render(
canvas,
'Countdown: ${countdown.current.toStringAsPrecision(3)}',
Vector2(30, 100),
);
textConfig.render(canvas, 'Elapsed time: $elapsedSecs', Vector2(30, 130));
}
}