如何为LIBGDX的scene2d ui创建Stage的“独立”实现

时间:2015-08-09 13:57:37

标签: libgdx imagebutton scene2d

我一直试图用一个按钮创建一个“主菜单”屏幕。但是,如果为舞台设置了视口,则此按钮是“无法点击的”。如果未设置视口,则该按钮变为可点击。然而,第二种方法是图像根据设备的分辨率“改变尺寸”。因此它们在较高分辨率的设备上变小,反之亦然。 以下是供您参考的代码:

 mainMenu(Game mainGame){
    camera = new OrthographicCamera();
    menuCam = new OrthographicCamera();
    allVariables.Graphics_viewport_height = (allVariables.Graphics_viewport_width*Gdx.graphics.getHeight())/Gdx.graphics.getWidth();
    camera.setToOrtho(false, allVariables.Graphics_viewport_width, allVariables.Graphics_viewport_height);
    camera.update();
    menuCam.setToOrtho(false,allVariables.Graphics_viewport_width,allVariables.Graphics_viewport_height);
    menuCam.update();
    maingame = mainGame;
    batch = new SpriteBatch();
    stage = new Stage();
    stage.setViewport(new StretchViewport(allVariables.Graphics_viewport_width,allVariables.Graphics_viewport_height));
    stage.getViewport().setCamera(menuCam);
    table = new Table();
    ImageButton.ImageButtonStyle playButtonStyle = new ImageButton.ImageButtonStyle();
    playButtonStyle.imageDown =       AllAssets.instance.skin.getDrawable("play_up");
    playButtonStyle.imageUp = AllAssets.instance.skin.getDrawable("play_dn");
    playButtonStyle.pressedOffsetY = 10;
    playButtonStyle.pressedOffsetX = 10;
    play = new ImageButton(playButtonStyle);
    table.setFillParent(true);
    table.align(Align.top);
    table.add(play).minSize(100, 200);
    table.debug();
    stage.addActor(table);
    Gdx.input.setInputProcessor(stage);}

调整大小方法:

   public void resize(int width, int height) {
         stage.getViewport().update(allVariables.Graphics_viewport_width,allVariables.Graphics_viewport_height,true);
         }

应该做些什么才能使分辨率独立,以便按钮可以在不同的分辨率上工作,相对大小不依赖于实际像素。

1 个答案:

答案 0 :(得分:1)

因此,这个问题的答案在于,Libgdx中的视口有两个特定尺寸方面。第一个是你想要展示的“游戏世界的一部分”,第二个是“屏幕的一部分” “你想要展示你的世界(而不是你的世界的一部分)。 要告诉Libgdx您希望显示的世界的哪个部分可以使用视口的构造函数。所以:

new StretchViewport(worldViewportwidth,worldViewportheight)

现在,为了告诉Libgdx屏幕的哪一部分(屏幕的部分内容,全屏等)在视口上使用更新方法:

public void resize(int width, int height) {
         stage.getViewport().update(width,height,true);
}

现在,舞台应该能够接受输入(如果其他一切也正确),舞台在不同的设备上可以正确显示

相关问题