我正在使用Flutter Flame制作 2D 游戏。该库使用画布,如下所示:
start() {
var previous = Duration.ZERO;
window.onBeginFrame = (now) {
var recorder = new PictureRecorder();
var canvas = new Canvas(
recorder,
new Rect.fromLTWH(
0.0, 0.0, window.physicalSize.width, window.physicalSize.height));
Duration delta = now - previous;
if (previous == Duration.ZERO) {
delta = Duration.ZERO;
}
previous = now;
var t = delta.inMicroseconds / Duration.MICROSECONDS_PER_SECOND;
update(t);
render(canvas);
var deviceTransform = new Float64List(16)
..[0] = window.devicePixelRatio
..[5] = window.devicePixelRatio
..[10] = 1.0
..[15] = 1.0;
var builder = new SceneBuilder()
..pushTransform(deviceTransform)
..addPicture(Offset.zero, recorder.endRecording())
..pop();
window.render(builder.build());
window.scheduleFrame();
};
window.scheduleFrame();
}
值得注意的是,Flutter Flame 使用了一个 custom BindingBase
,类似于 Widgets 的工作方式。
class _CustomBinder extends BindingBase with ServicesBinding {}
这对游戏很有用,但我希望在主菜单、设置页面等上使用真正的颤振小部件。
有没有办法在这两个上下文之间交换?
为了提供我正在寻找的东西的想法,我希望存在这两个功能:
loadHomeScreen(); // replaces the canvas, if any, with Flutter widgets
loadCanvasScene(); // replaces the Flutter widgets with the canvas