Android studio LibGdx:如何为UI元素设置单独的视口?

Android studio LibGdx:如何为UI元素设置单独的视口?,android-studio,libgdx,Android Studio,Libgdx,我试图用一个使用操纵杆(触摸板)移动的角色来编写一个简单的迷宫。我使用了一个合适的视口来保持迷宫的纵横比,同时将其保持在屏幕的中心 然而,我发现在迷宫旁边也增加了操纵杆 我希望操纵杆位于屏幕左下角。我试着为UI元素(包括操纵杆)使用不同的视口来实现这一点,但随后整个迷宫就扭曲了 这是我的BaseScreen类的一部分,也是我在其他游戏中重用的扩展: public abstract class BaseScreen implements Screen, InputProcessor { publ

我试图用一个使用操纵杆(触摸板)移动的角色来编写一个简单的迷宫。我使用了一个合适的视口来保持迷宫的纵横比,同时将其保持在屏幕的中心

然而,我发现在迷宫旁边也增加了操纵杆

我希望操纵杆位于屏幕左下角。我试着为UI元素(包括操纵杆)使用不同的视口来实现这一点,但随后整个迷宫就扭曲了

这是我的BaseScreen类的一部分,也是我在其他游戏中重用的扩展:

public abstract class BaseScreen implements Screen, InputProcessor {

public BaseScreen(){

    gameWorldHeight = 1024;
    gameWorldWidth = 1024;
    cam = new OrthographicCamera(gameWorldWidth, gameWorldHeight);
    port = new FitViewport(cam.viewportWidth, cam.viewportHeight, cam);
    port.apply();
    cam.update();

    mainStage = new Stage(port);
    uiStage = new Stage(port);
    uiTable = new Table();
    uiTable.setFillParent(true);
    uiStage.addActor(uiTable);

    initialize();
}

public abstract void initialize();

public void render(float dt) {
    mainStage.getCamera().update();
    uiStage.getCamera().update();

    uiStage.act(dt);
    mainStage.act(dt);

    update(dt);

    mainStage.draw();
    uiStage.draw();
}

public abstract void update(float dt);

public void resize(int width, int height) {
    mainStage.getViewport().update(width,height, true);
    uiStage.getViewport().update(width,height,false);
}
}
这里也是我的gameScreen类的一部分,我实际上编写了游戏的所有核心机制

public class gameScreen extends BaseScreen {

@Override
public void initialize() {

    //..

    uiTable.pad(10);
    uiTable.add().expandX().expandY();
    uiTable.row();
    uiTable.add(ball.touchpad).left();
    uiTable.add().expandX();
  }
}
如果你想知道,这里是什么球。触摸板

public class Ball extends BaseActor {

public Touchpad touchpad;
public Touchpad.TouchpadStyle touchpadStyle;
public Skin touchpadSkin;
public Drawable touchBackground;
public Drawable touchKnob;

public Ball (float x, float y, Stage s) {

    //..


    //Create a touchpad skin
    touchpadSkin = new Skin();

    //Set background image
    touchpadSkin.add("touchBackground", new Texture("touchBackground.png"));

    //Set knob image
    touchpadSkin.add("touchKnob", new Texture("touchKnob.png"));

    //Create TouchPad Style
    touchpadStyle = new Touchpad.TouchpadStyle();

    //Create Drawable's from TouchPad skin
    touchBackground = touchpadSkin.getDrawable("touchBackground");
    touchKnob = touchpadSkin.getDrawable("touchKnob");

    //Apply the Drawables to the TouchPad Style
    touchpadStyle.background = touchBackground;
    touchpadStyle.knob = touchKnob;

    //Create new TouchPad with the created style
    touchpad = new Touchpad(10, touchpadStyle);
    //setBounds(x,y,width,height)
    touchpad.setBounds(15, 15, 200, 200);

}

那么如何在屏幕左下角添加此触摸板?

为UI使用单独的阶段。这是唯一的方法,如果你正在使用阶段的游戏视图以及。根据我的经验,LibGDX Scene2D对于UI来说非常好,但是通常很难用于游戏视图和逻辑本身。不过,这取决于游戏的类型。@Tenfour04是的,我有一个单独的uiStage。但当我将一个单独的视口应用到这个阶段时,它显然会扭曲主视口。所以我不知道如何解决这个问题。