I'm trying to make a RPG style game using Flame 1.8.
This is my onLoad of my game (extends FlameGame):
我正在尝试用Flame 1.8制作一款RPG风格的游戏。这是我的游戏(扩展了FlameGame):
@override
Future<void> onLoad() async {
// debugMode = true;
myWorld = MyWorld();
await add(myWorld);
Flame.device.setLandscape();
cameraComponent = CameraComponent(world: myWorld);
//cameraComponent.viewfinder.anchor = Anchor.topLeft;
await add(cameraComponent);
return super.onLoad();
}
They say you should use this to get the job done:
他们说,你应该用这个来完成工作:
camera.viewport = FixedResolutionViewport(size);
But in Flame 1.8 there is no camera property and CamerComponent view port seems to be readonly.
So how can I set my game to fit the device screen size. It works very good on Chrome I just do the math using myGame.size and everthing is where it should be but when I test it on android device some of the components are not shown which should be because their position's top is something like myGame.size.y - (components.size.height) , for example consider this :
但在Flame 1.8中,没有相机属性,CamerComponent的视图端口似乎是只读的。那么,我如何设置我的游戏以适应设备的屏幕大小呢?它在Chrome上运行得很好,我只是用myGame.Size来计算,所有东西都应该在它应该在的地方,但当我在Android设备上测试它时,一些组件没有显示出来,这应该是因为它们的位置的顶部类似于myGame.size.y-(Components.size.Height),例如,考虑一下:
for (int i = 0; i * 1024 < 20000; i++) {
var top = game.size.toSize().height - 64;
add(Brick(position: Vector2(i * 1024, top)));
}
It's in bottom of the screen on firefox but in android device it's not shown and it's like I've done zoom or something ,thank you very much
它在Firefox的屏幕底部,但在Android设备上没有显示,就像我做了缩放或其他什么,非常感谢
PS: 64 is the brick's height
PS:64是砖的高度
PS2: I said viewport is readonly because Code.exe is saying so:
PS2:我说视区是只读的,因为Code.exe是这么说的:
'viewport' can't be used as a setter because it's final. Try finding a
different setter, or making 'viewport' non-final
But this solved my problem :
但这解决了我的问题:
final cameraComponent = CameraComponent.withFixedResolution( world: myWorldComponent, width: size.x, height: size.y, );
Thank you very much,
非常感谢,
更多回答
优秀答案推荐
The viewport for the CameraComponent
needs to passed in to the CameraComponent
constructor, like this:
CameraComponent的视区需要传递给CameraComponent构造函数,如下所示:
final cameraComponent = CameraComponent(
world: world,
viewport: <Your viewport>,
);
or for the FixedResolutionViewport
you can also use the other CameraComponent
constructor and it will set up the viewport for you:
或者,对于FixedResolutionViewport,您也可以使用其他CameraComponent构造函数,它将为您设置视区:
final cameraComponent = CameraComponent.withFixedResolution(
world: myWorldComponent,
width: 800,
height: 600,
);
更多回答
Thank you for the withFixedResolution . You saved me . I almost read the whole document but unfortunately missed this. But VS Code is not agree with the first part : 'viewport' can't be used as a setter because it's final. Try finding a different setter, or making 'viewport' non-final
感谢您的带固定的解决方案。你救了我。我几乎读完了整篇文档,但不幸的是错过了这一点。但是VS代码不同意第一部分:‘viewport’不能用作setter,因为它是最终的。尝试寻找不同的设置者,或使‘view’不是最终的
@Vahid2023 aah true, it actually is final, I updated my answer.
@Vaid20aah是真的,实际上是最终的,我更新了我的答案。
我是一名优秀的程序员,十分优秀!