mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-04 04:47:13 +08:00
* Improving TimerComponent API * pr suggestion * Update packages/flame/lib/src/components/timer_component.dart Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com> * pr suggestion * docs * pr suggestions * fixing * renaming to onTick * Update packages/flame/lib/src/components/timer_component.dart Co-authored-by: Luan Nico <luanpotter27@gmail.com> * Apply suggestions from code review Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com> * fixing issues * fixing flmae bloc isse * more suggestions * Update packages/flame/lib/src/timer.dart Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com> Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com> Co-authored-by: Luan Nico <luanpotter27@gmail.com>
50 lines
1.0 KiB
Dart
50 lines
1.0 KiB
Dart
import 'package:flame/game.dart';
|
|
import 'package:flame/input.dart';
|
|
import 'package:flame/timer.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class TimerGame extends FlameGame with TapDetector {
|
|
final TextPaint textConfig = TextPaint(
|
|
style: const TextStyle(color: Colors.white),
|
|
);
|
|
late Timer countdown;
|
|
late Timer interval;
|
|
|
|
int elapsedSecs = 0;
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
await super.onLoad();
|
|
countdown = Timer(2);
|
|
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}',
|
|
Vector2(10, 100),
|
|
);
|
|
textConfig.render(canvas, 'Elapsed time: $elapsedSecs', Vector2(10, 150));
|
|
}
|
|
}
|