Skip to content

Commit 0113aa3

Browse files
erickzanardospydon
andauthored
feat: Adding tickOnLoad to TimerComponent (#3285)
Adds a `tickOnLoad` method to `TimerComponent`. --------- Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
1 parent 76a9aba commit 0113aa3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

packages/flame/lib/src/components/timer_component.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import 'dart:async';
12
import 'dart:ui';
23

34
import 'package:flame/components.dart';
5+
import 'package:meta/meta.dart';
46

57
/// A component that uses a [Timer] instance which you can react to when it has
68
/// finished.
79
class TimerComponent extends Component {
810
late final Timer timer;
911
final bool removeOnFinish;
1012
final VoidCallback? _onTick;
13+
final bool tickWhenLoaded;
1114

1215
/// Creates a [TimerComponent]
1316
///
@@ -16,12 +19,15 @@ class TimerComponent extends Component {
1619
/// [autoStart] When true, will start upon instantiation (default is true)
1720
/// [onTick] When provided, will be called every time [period] is reached.
1821
/// This overrides the [onTick] method
22+
/// [tickWhenLoaded] When true, will call [onTick] when the component is first
23+
/// loaded (default is false).
1924
TimerComponent({
2025
required double period,
2126
bool repeat = false,
2227
bool autoStart = true,
2328
this.removeOnFinish = false,
2429
VoidCallback? onTick,
30+
this.tickWhenLoaded = false,
2531
super.key,
2632
}) : _onTick = onTick {
2733
timer = Timer(
@@ -32,6 +38,16 @@ class TimerComponent extends Component {
3238
);
3339
}
3440

41+
@override
42+
@mustCallSuper
43+
FutureOr<void> onLoad() async {
44+
await super.onLoad();
45+
46+
if (tickWhenLoaded) {
47+
onTick();
48+
}
49+
}
50+
3551
/// Called every time the [timer] reached a tick.
3652
/// The default implementation calls the closure received on the
3753
/// constructor and can be overridden to add custom logic.

packages/flame/test/components/timer_component_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ class _MyTimerComponent extends TimerComponent {
1818
}
1919
}
2020

21+
class _MyTickOnLoadTimerComponent extends TimerComponent {
22+
int count = 0;
23+
24+
_MyTickOnLoadTimerComponent()
25+
: super(
26+
period: 1,
27+
repeat: true,
28+
removeOnFinish: false,
29+
tickWhenLoaded: true,
30+
);
31+
32+
@override
33+
void onTick() {
34+
count++;
35+
}
36+
}
37+
2138
class _NonRepeatingTimerComponent extends TimerComponent {
2239
_NonRepeatingTimerComponent()
2340
: super(
@@ -32,6 +49,7 @@ void main() {
3249
testWithFlameGame('runs the tick method', (game) async {
3350
final timer = _MyTimerComponent();
3451
game.add(timer);
52+
await game.ready();
3553
game.update(0);
3654

3755
game.update(1.2);
@@ -44,6 +62,7 @@ void main() {
4462
(game) async {
4563
final world = game.world;
4664
world.add(_MyTimerComponent());
65+
await game.ready();
4766
game.update(0);
4867

4968
game.update(1.2);
@@ -74,10 +93,28 @@ void main() {
7493
},
7594
),
7695
);
96+
await game.ready();
7797
game.update(0);
7898
game.update(1.2);
7999

80100
expect(called, isTrue);
81101
});
102+
103+
testWithFlameGame(
104+
'runs the tick method on load when tickWhenLoaded is true',
105+
(game) async {
106+
final timer = _MyTickOnLoadTimerComponent();
107+
game.add(timer);
108+
await game.ready();
109+
expect(timer.count, equals(1));
110+
111+
game.update(0);
112+
113+
game.update(1.2);
114+
115+
game.update(0);
116+
expect(timer.count, equals(2));
117+
},
118+
);
82119
});
83120
}

0 commit comments

Comments
 (0)