屏幕 在某些屏幕上显示不正确 Libgdx

Screen Display not proper on some screens Libgdx

我正在开发一款游戏,我正在制作 游戏结束屏幕,但组件没有正确重新排列。

我在 1024 * 720 屏幕上看到的是:

它应该是什么样子:

代码是:

@Override
public void show() {
    stage = new Stage(new ScreenViewport());
    camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    Gdx.input.setInputProcessor(stage);
    font  = new BitmapFont(Gdx.files.internal("newfont.fnt"));
    verysmallfont = new BitmapFont(Gdx.files.internal("verysmallfont.fnt"));
    smallfont = new BitmapFont(Gdx.files.internal("smallfont.fnt"));

    atlas = new TextureAtlas("ui/buttons.pack");//add atlas

    skin = new Skin(atlas);

    table = new Table(skin);
    actiontable = new Table(skin);
    actionbar = new Table(skin);
    actionbar2 = new Table(skin);

    table.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    actiontable.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    actionbar.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    actionbar2.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());


    TextButton.TextButtonStyle buttonback = new TextButton.TextButtonStyle();
    buttonback.up = skin.getDrawable("back");
    buttonback.pressedOffsetX = 1;
    buttonback.pressedOffsetY = -1;
    buttonback.font = font;

    buttonBack = new TextButton("",buttonback);

    TextButton.TextButtonStyle lifebuttonstyle = new TextButton.TextButtonStyle();
    lifebuttonstyle.up = skin.getDrawable("life");
    lifebuttonstyle.pressedOffsetX = 1;
    lifebuttonstyle.pressedOffsetY = -1;
    lifebuttonstyle.font = font;

    buttonlife = new TextButton("",lifebuttonstyle);

    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.up = skin.getDrawable("heart_game_continue");
    textButtonStyle.pressedOffsetX = 1;
    textButtonStyle.pressedOffsetY = -1;
    textButtonStyle.font = font;

    buttonPlay = new TextButton("",textButtonStyle);

    TextButton.TextButtonStyle adsfreeStyle = new TextButton.TextButtonStyle();
    adsfreeStyle.up = skin.getDrawable("Video");
    adsfreeStyle.pressedOffsetX = 1;
    adsfreeStyle.pressedOffsetY = -1;
    adsfreeStyle.font = font;

    buttonVideo = new TextButton("",adsfreeStyle);



    TextButton.TextButtonStyle sharebuttonStyle = new TextButton.TextButtonStyle();
    sharebuttonStyle.up = skin.getDrawable("Replay");
    sharebuttonStyle.pressedOffsetX = 1;
    sharebuttonStyle.pressedOffsetY = -1;
    sharebuttonStyle.font = font;

    buttonReplay = new TextButton("",sharebuttonStyle);



    Label.LabelStyle headingstyle = new Label.LabelStyle(font,Color.WHITE);

    label = new Label("Game Over",headingstyle);
    label.setFontScale(1.7f);

    Label.LabelStyle contstyle  = new Label.LabelStyle(smallfont,Color.WHITE);
    cont = new Label("Continue?",contstyle);
    cont.setFontScale(2f);

    Label.LabelStyle replaystyle = new Label.LabelStyle(verysmallfont,Color.WHITE);
    replay = new Label("Replay",replaystyle);
    shortVideo = new Label("(Short Video)",replaystyle);
    replay.setFontScale(2f);
    shortVideo.setFontScale(2f);


    table.align(Align.top);
    table.padTop(197f);
    table.add(label);
    table.getCell(label).spaceBottom(150f);
    table.row();
    table.add(cont);
    table.getCell(cont).spaceBottom(80f);
    table.row();
    table.add(buttonPlay).size(200f,200f);


    actiontable.add(buttonVideo).size(200f,200f);
    actiontable.add(buttonReplay).size(200f,200f);
    actiontable.align(Align.bottom);
    actiontable.getCell(buttonVideo).spaceBottom(20f).padRight(100f);
    actiontable.getCell(buttonReplay).spaceBottom(20f).padLeft(100f);
    actiontable.row();
    actiontable.add(shortVideo).padRight(100f);
    actiontable.add(replay).padLeft(100f);
    actiontable.padBottom(197f);

    actiontable.setFillParent(true);

    actionbar.align(Align.topLeft).setWidth(Gdx.graphics.getWidth());
    actionbar.add(buttonBack).align(Align.left).size(90f,90f);

    actionbar2.align(Align.topRight);
    actionbar2.add(buttonlife).align(Align.right).size(90f,90f);

    actionbar.getCell(buttonBack).pad(43f);
    actionbar2.getCell(buttonlife).align(Align.right).pad(43f);

    stage.addActor(actionbar2);
    stage.addActor(actionbar);
    stage.addActor(table);
    stage.addActor(actiontable);

    buttonPlay.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            ((Game) Gdx.app.getApplicationListener()).setScreen(new Main(level,a,start,sweep,collide,innerarcs,out,mid,arcsleft,left,pointerColor));
        };
    });

    buttonReplay.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            int total = out+mid+innerarcs;
            ((Game) Gdx.app.getApplicationListener()).setScreen(new Main(level,a,total,pointerColor));
        }
    });

    buttonBack.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            ((Game) Gdx.app.getApplicationListener()).setScreen(new Menu(level));
        }
    });


}
SpriteBatch batch;
@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.184f,0.184f,0.184f,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();

}
private OrthographicCamera camera;

@Override
public void resize(final int width, final int height) {
    table.setTransform(true);
    table.setSize(width,height);
    actiontable.setTransform(true);
    actiontable.setSize(width,height);
    actionbar.setTransform(true);
    actionbar.setSize(width,height);

}

我是 libgdx 的新手,请帮助我,我对如何使用相机和视口一无所知。

提前致谢..

€dit:对不起我的错 - 没有什么比 "stage.resize" 但我会给出答案,因为它会解决你的问题,虽然不会像你希望的那样快,但会以更清晰的方式。

(以下文字不是回答所必需的,只是建议)

(如果您遵循以下步骤,您将在以后节省时间和头痛)

我建议使用 viewport,因为它们会解决很多缩放问题,而且有很多,您可以根据需要从中选择。 LibGdx Viewports

视口在不同技术(例如缩放、黑条)的帮助下照顾不同的屏幕尺寸 此外,视口非常容易实现。

您将执行以下操作(例如 FitViewport(拉伸内容,如有必要)):

stage = new Stage();
OrthographicCamera cam = new OrthographicCamera();
cam.setToOrtho(false, 800, 600);
stage.setViewport(new FitViewport(800, 600, cam));

多田。您实现了一个视口。没那么难。 (但一定要在 resize(w, h) 方法中调用 stage.getViewport().update(w, h)!! 别忘了 !!)

我的第二个建议是: 使用主 table。 所以你关注

Table myMothershipTable = new Table();
myMothershipTable.setFillParent(true);
myMothershipTable.add(add);
myMothershipTable.add(all);
myMothershipTable.add(content);
stage.addActor(myMothershipTable);

现在你只有一个演员,就是 table,你可以确定,这个 table 会填满整个屏幕。

你的屏幕是10999x3?是的。 table 将是 10999x3。

很好。如果您已将所有内容重构为一个 table 我建议下一步:

stage.setDebugAll(true); //this will help you to see the actual buildup from your actors & elements

原因如下:

  1. 视口会照顾您的舞台大小
  2. 单个 table 确保您的完整内容将在屏幕内 ==> 现在您可以一个接一个地添加项目,并确保使用..
  3. stage.setDebugAll(true)..您的内容放置正确。

如果您遵循这些简单的步骤,您将在设计 UI 时节省大量时间和头痛。