【问题标题】:Screen Display not proper on some screens Libgdx某些屏幕上的屏幕显示不正确 Libgdx
【发布时间】:2016-05-18 13:17:36
【问题描述】:

我正在开发一款游戏,其中我正在制作Game Over Screen,但组件没有正确重新排列。

我在 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 的新手,请帮助我对如何使用相机和视口一无所知。

提前谢谢..

【问题讨论】:

    标签: libgdx game-engine game-center


    【解决方案1】:

    €dit:对不起,我的错 - 没有像“stage.resize”这样的东西 但我会告诉你答案,因为它会解决你的问题,虽然没有你希望的那么快,但会以一种更清晰的方式。

    以下文字对于回答者来说不是必需的,只是建议

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

    我建议使用 视口,因为它们会解决很多缩放问题,而且有很多问题可供您根据需要进行选择。 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 myMothershipTable = new Table();
    myMothershipTable.setFillParent(true);
    myMothershipTable.add(add);
    myMothershipTable.add(all);
    myMothershipTable.add(content);
    stage.addActor(myMothershipTable);
    

    现在你只有一个演员,那就是桌子,你可以肯定,这张桌子会填满整个屏幕。

    您的屏幕是 10999x3?是的。该表将为 10999x3。

    很好。如果您已将所有内容重构为一张表,我建议您进行下一步:

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

    这就是为什么:

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

    如果您按照这些简单的步骤操作,您将节省大量时间和设计 UI 的麻烦。

    【讨论】:

    • 感谢我现在要实施它的建议,但我想知道它能否解决调整我的资产大小的问题,正如您在上图中看到的那样,按钮大小没有受到影响...??
    • 我认为编写干净的代码会有所帮助。我想你的问题是,多个因素共同作用。但我需要说,我从来没有像你这样遇到过这样的问题。这就是为什么我喜欢我的方法。你不应该忘记的唯一一件事:stage.getViewport().update(w,h)
    • 感谢您的指导..:) 我使用视口和相机解决了它..它现在可以工作了..
    猜你喜欢
    • 1970-01-01
    • 2013-07-05
    • 2020-11-11
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多