我对颤动和火焰仍然很陌生,但我正在开发一个游戏,我需要能够在扩展 BaseGame 的类中传递 BuilerContext 上下文,我目前正在使用一种方法,该方法将作为小部件的组件添加到屏幕称为“添加WidgetOverlay”。我知道这个错误通常是在小部件内部未调用 BuilderContext 上下文时引起的,但我不完全确定我的类在哪里扩展 BaseGame 而不是有状态或无状态小部件。提前致谢!
import 'package:flame/components/parallax_component.dart';
import 'package:flame/components/text_component.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'gamesfdafdsafdsa.dart';
class NewMenu extends BaseGame with HasWidgetsOverlay {
final SharedPreferences storage;
TextComponent _text;
ParallaxComponent _parallaxComponent;
// BuildContext context;
NewMenu(this.storage, BuildContext context) {
// TEMP BACKGROUND
_parallaxComponent = ParallaxComponent([
ParallaxImage('Layer_0010_1.png'),
], baseSpeed: Offset(100, 0), layerDelta: Offset(20, 0));
// ADDS THE PLAY BUTTON
addWidgetOverlay('Play Button',
Center(
child: TextButton(
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => GamePlay(),
),
);
}, child: Text('Play')),
),
);
// PARALLAX COMPONENT
add(_parallaxComponent);
// HIGHSCORE TEXT
_text = TextComponent('${storage.getInt('highscore') ?? 0}');
add(_text);
}
}