动态地将游戏从纵向更改为横向 [英] Change game from portrait to landscape dynamically

查看:97
本文介绍了动态地将游戏从纵向更改为横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个实现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.而新的Activityintialize 播放屏幕.但是显然,您不能调用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()对齐SpritesActors,因此在不同的设备上看起来正确".

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中的主菜单:

ActorsSprites的位置和大小均基于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆