Search code examples
libgdxgame-enginegame-center

Screen Display not proper on some screens Libgdx


I am developing a game in which i am making a Game Over Screen but the components are not rearranging properly.

What i am Getting on 1024 * 720 screen is: enter image description here

and what it should look like:

enter image description here

and the code is:

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

}

I am new to libgdx please help i dont have any knowledge about how to use camera and viewports.

Thanks in advance..


Solution

  • €dit: sorry my bad - there's nothing like "stage.resize" But I'll let the answer in, because it'll fix your problem, though not as fast as you wish, but in a more clear way.

    (THE FOLLOWING TEXT IS NOT NECESSARRY FOR THE ANWSER AND ARE ONLY SUGGESTIONS)

    (if you follow the following steps, you'll save yourself time & headaches in the future)

    I'd suggest a viewport, because they'll kill many problems with scaling, and there are quite a few, from which you can choose, as you need. LibGdx Viewports

    A viewport takes care for different screen sizes with the help from different techniques (e.g. scaling, black bars) Additionally a viewport is SUPER easy to implement.

    You'll do the following(e.g. FitViewport(stretches content, if necessary)):

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

    TADA. You implemented a viewport. Was not so hard. (but make sure to call stage.getViewport().update(w, h) in the resize(w, h) method!! AND DON'T FORGET IT!!)

    Second suggestion I'd do is: Use a main table. So you do following

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

    Now you've only one actor, which is the table, and you can be damned sure, that this table will fill the whole screen.

    Your screen is 10999x3? yeah. the table will be 10999x3.

    Good. If you've refactored your whole stuff into one table I'd suggest the next step:

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

    So heres the why:

    1. The viewport takes care for your stage size
    2. the single table makes sure, that your complete content will be within the screen ==> now you can add one item after another, and make sure with..
    3. stage.setDebugAll(true).. that your content is placed properly.

    If you follow this simple steps, you'll save yourself a lot of time and headache with designing the UI.