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

时间:2018-07-27 23:47:31

标签: android-studio libgdx

我正在尝试编写一个简单的迷宫,其角色要使用操纵杆(触摸板)移动。我使用Fit视口在保持迷宫的纵横比的同时将其保持在屏幕中央;

但是,我发现在迷宫旁边也添加了操纵杆。 sample of what it looks like currently

我希望操纵杆位于屏幕的左下角。我尝试对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();
  }
}

如果您想知道,这是ball.touchpad是

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);

}

那我如何在屏幕左下方添加此触摸板?

0 个答案:

没有答案