动态地将游戏从纵向更改为横向 [英] Change game from portrait to landscape dynamically
问题描述
我有多个实现Screen
的类.
例如,我想拥有主菜单屏幕portrait
,然后是播放屏幕 landscape
.无论如何,可以动态更改orientation
吗?
I want to, say, have the Main Menu screen portrait
, then the Play Screen landscape
. Is there anyway of changing the orientation
dynamically?
我以为我可以再启动一个Activity
并让播放屏幕扩展Game
.而新的Activity
将intialize
播放屏幕.但是显然,您不能调用startActivity
之类的东西,因为LibGDX
是平台无关的.
I thought I could just start another Activity
and have the Play Screen extend Game
. And the new Activity
would intialize
the Play Screen. But it appears that you can't call startActivity
or the like, obviously, as LibGDX
is platform independent.
我使用Gdx.graphics.getWidth()
和Gdx.graphics.getHeight()
对齐Sprites
和Actors
,因此在不同的设备上看起来正确".
I use Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
to align Sprites
and Actors
, so it looks "right" on different devices.
我也分别尝试过交换Gdx.graphics.getWidth()
和Gdx.graphics.getHeight()
,但是似乎无法正确地确定精灵和角色的位置.
I've also tried swapping the Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
, respectively, but can't seem to get the position of sprites and actors correct.
而且,在我的清单中,我有orientation="landscape"
.
Also, in my manifest I have orientation="landscape"
.
有些图像应该更有意义.
Here are some images that should make more sense.
这是我在landscape
中的主菜单:
这是我在portrait
中的主菜单:
Actors
和Sprites
的位置和大小均基于Gdx.graphics.getWidth()
和Gdx.graphics.getHeight()
.在这两个图像之间唯一改变的是清单中的orientation="landscape"
和orientation="portrait"
.
The position and size of Actors
and Sprites
are all based on Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
. The only thing that changes between the two images is orientation="landscape"
and orientation="portrait"
in my manifest.
现在,我要在portrait
中显示我的主菜单,但是要在其中打开游戏的播放屏幕应该在landscape
中.
Now I want my Main Menu in portrait
but the Play Screen, where the game is on, should be in landscape
.
我最初开始在播放屏幕以及基于orientation="landscape"
设置的大小和位置上进行工作,现在我在主菜单上进行工作基于orientation="portrait"
.因此,为什么要动态更改方向.
I initially started working on the Play Screen and the sizes and positions where set based on orientation="landscape"
, and now that I am working on the Main Menu it is based on orientation="portrait"
. Hence why I want to change the orientation dynamically.
推荐答案
您不应有多个Activities
.要了解如何以编程方式切换方向,您可以查看此链接.
You should not have multiple Activities
. To learn how to switch the orientation programmatically you can have a look at this link.
正如您所说,LibGDX是独立于平台的.这就是为什么您无法从核心"代码访问Android- Activity
的原因.要了解如何解决此问题,请查看与特定平台的接口代码.
As you said, LibGDX is platform-independent. That's why you cannot access your Android-Activity
from your "core" code. To learn how to workaround that, have a look at Interfacing with platform specific code.
在执行Screen
转换时调用界面更改游戏方向时,LibGDX将使用新值自动调用resize(width, height)
,并且您可以根据新值对齐Stage
等.
When you call your interface to change the orientation from your game when doing a Screen
transition, LibGDX will automatically call resize(width, height)
with the new values and you can align your Stage
and so on according to the new values.
这篇关于动态地将游戏从纵向更改为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!