mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-31 00:48:47 +08:00
55 lines
1.2 KiB
Dart
55 lines
1.2 KiB
Dart
import 'package:flame/events.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame/timer.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class TimerExample extends FlameGame with TapCallbacks {
|
|
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 {
|
|
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));
|
|
}
|
|
}
|