简体   繁体   English

动态将游戏从纵向更改为横向

[英]Change game from portrait to landscape dynamically

I have multiple classes that implement Screen . 我有多个实现Screen类。

I want to, say, have the Main Menu screen portrait , then the Play Screen landscape . 我想说, 主菜单屏幕为portrait ,然后为播放屏幕为 landscape Is there anyway of changing the orientation dynamically? 无论如何,有没有动态改变orientation

I thought I could just start another Activity and have the Play Screen extend Game . 我以为我可以开始另一个Activity并让Play Screen扩展Game And the new Activity would intialize the Play Screen . 并且新的Activityintialize 播放屏幕 But it appears that you can't call startActivity or the like, obviously, as LibGDX is platform independent. 但是显然,您不能调用startActivity之类的东西,因为LibGDX是平台无关的。

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()来对齐SpritesActors ,因此在不同的设备上看起来“正确”。

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. 我也尝试过分别交换Gdx.graphics.getWidth()Gdx.graphics.getHeight() ,但似乎无法正确地确定精灵和角色的位置。

Also, in my manifest I have orientation="landscape" . 另外,在我的清单中,我的orientation="landscape"

Here are some images that should make more sense. 这是一些应该更有意义的图像。

So this is my Main Menu in landscape : 这是我在landscape 主菜单 在此处输入图片说明

And this is my Main Menu in portrait : 这是我的portrait 主菜单

在此处输入图片说明

The position and size of Actors and Sprites are all based on Gdx.graphics.getWidth() and Gdx.graphics.getHeight() . ActorsSprites的位置和大小均基于Gdx.graphics.getWidth()Gdx.graphics.getHeight() The only thing that changes between the two images is orientation="landscape" and orientation="portrait" in my manifest. 这两个图像之间唯一发生变化的是清单中的orientation="landscape"orientation="portrait"

Now I want my Main Menu in portrait but the Play Screen , where the game is on, should be in landscape . 现在,我希望我的主菜单portrait 屏幕 ,但要进行游戏的“ 播放屏幕”应为landscape 屏幕

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" . 最初,我开始在“ 播放屏幕 orientation="landscape" ,并根据orientation="landscape"设置大小和位置,现在我在主菜单上进行的操作是基于orientation="portrait" Hence why I want to change the orientation dynamically. 因此,为什么要动态更改方向。

This is a good question :) I develop noone's answer (which is correct) 这是一个好问题:)我没有人回答(正确)

How to set lanscape screen with Android sdk specific classes ? 如何使用Android SDK特定类设置lanscape屏幕?

And how to import android classes in a libgdx project ? 以及如何在libgdx项目中导入android类?

First of all note that Android classes can only be imported in the android project and not in the core. 首先请注意,Android类只能在android项目中导入,而不能在核心中导入。 So there we save the Activity in a platform dependant object. 因此,我们将Activity保存在依赖于平台的对象中。 Here is my AndroidLauncher: 这是我的AndroidLauncher:

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.util.Platform;

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        PlatformAndroid platform = new PlatformAndroid();
        platform.setActivity(this);
        initialize(new MyGdxGame((Platform) platform ), cfg);
}

}

PlatformAndroid implements Platform. PlatformAndroid实现Platform。 These two classes are made to access platform specific objects. 这两个类用于访问平台特定的对象。 Let's look at them : 让我们看看他们:

public interface Platform {
   public void SetOrientation(String string);
}

and

package com.mygdx.game.android;
import android.app.Activity;
import android.content.pm.ActivityInfo;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.mygdx.game.util.Platform;

public class PlatformAndroid extends AndroidApplication implements Platform {
    private Activity activity;
    @Override
    public void SetOrientation(String string) {
       if (string == "landscape"){
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       }
    }
    public void setActivity(Activity activity){ this.activity = activity;   }
}  

You can also create a PLatformDesktop called by the desktop launcher. 您还可以创建由桌面启动器调用的PLatformDesktop。 Anyway, keep the platform variable in your classes and when you want to set the screen to landscape just call: 无论如何,请将platform变量保留在您的类中,并且要将屏幕设置为横向时,只需调用:

 platform.SetOrientation("landscape");

You should not have multiple Activities . 您不应有多个Activities To learn how to switch the orientation programmatically you can have a look at this link . 要了解如何以编程方式切换方向,可以查看此链接

As you said, LibGDX is platform-independent. 如您所说,LibGDX是平台无关的。 That's why you cannot access your Android- Activity from your "core" code. 这就是为什么您无法从“核心”代码访问Android Activity的原因。 To learn how to workaround that, have a look at Interfacing with platform specific code . 要了解如何解决该问题,请查看与平台特定代码的接口

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. 当您在执行Screen过渡时调用界面来更改游戏的方向时,LibGDX会自动使用新值调用resize(width, height) ,您可以根据新值对齐Stage等。

I had the same problem and this solved it: 我有同样的问题,这解决了它:

@Override
public void resize(int width, int height) {
    stage.getViewport().setScreenSize(width, height);
}

For some reason, even though the screen resolution changed, the stage didn't know that. 由于某种原因,即使屏幕分辨率发生了变化,舞台也不知道这一点。 So I've told it to the stage. 所以我已经讲到舞台了。

Just create a interface in core project and and declare a method with any name than implement that interface in your AndroidLauncher class. 只需在核心项目中创建一个接口,然后声明一个名称,然后使用您在AndroidLauncher类中实现该接口的任何方法即可。 Give definition of your method. 给出方法的定义。

For me GameOrien.java is interface having method declaration 对我来说, GameOrien.java是具有方法声明的接口

public interface GameOrien {

    public static final int PORTRAIT=1;

    public static final int LANDSCAPE=2;

    void setOrientation(int orientation);
}

In AndroidLauncher.java class which is in Android project. AndroidLauncher.java类中,它在Android项目中。

public class AndroidLauncher extends AndroidApplication implements GameOrien {

**...**

    @Override
    public void setOrientation(int orientation) {

        if(orientation==PORTRAIT)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        else if(orientation==LANDSCAPE)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

}

When you want to change orientation for GamePlay just call setOrientaion() method of interface. 当您想更改GamePlay的方向时,只需调用接口的setOrientaion()方法即可。 Your orientation changed. 您的方向已改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM