【发布时间】:2021-09-29 13:31:01
【问题描述】:
我刚刚从 Flame 1.0.0-rc8 更新到 Flame 1.0.0-releasecandidate.14,但现在我的叠加层(菜单、分数等)无法在不闪烁游戏的情况下刷新。
这是我的代码简历:
class _Boat extends State<Boat> with WidgetsBindingObserver {
BoatGame game;
@override
void initState() {
game = new BoatGame();
refreshScore();
}
refreshScore() async {
timer = Timer.periodic(Duration(milliseconds: 300), (timer) {
if (mounted) {
if (game != null) {
score = game.getScore();
setState(() {});
}
}
});
}
@override
Widget build(BuildContext context) {
Stack(
children: [
GameWidget(game: game,),
Text("My Score: $score");
],
}
}
因此,每 300 毫秒,我的屏幕会闪烁一次,如果我删除 SetState,则不会刷新分数。
解决办法是什么?谢谢
【问题讨论】:
-
我不知道 Flame,但您是否考虑过使用
ValueNotifier而不是使用setState更新您的小部件?它会有所帮助,因为只有保存分数的Text小部件会被更新。