Search code examples
libgdx

Libgdx - align vertical group to center of the stage?


This seems like a very simple question, but I can't seem to find an easy answer. I simply have a VerticalGroup that I'd like to place in the center of the stage. Most containers seems to do this naturally but this one doesn't. It's just aligned at the top of the view by default. It also doesn't respond to setting "align.bottom", just left and right. Any ideas?

        stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, spriteBatch);

  //VericalGroup the buttons and the label
     VerticalGroup group = new VerticalGroup();
     group.setFillParent(true);
     group.addActor(pennyLabel);
     group.addActor(table);

     stage.addActor(group);

enter image description here


Solution

  • try this simple test:

    group.setOrigin(group.getWidth() /2, 
                    group.getHeight()/2);
    
    group.setPosition(Gdx.graphics.getWidth() /2 - (group.getWidth() /2),
                      Gdx.graphics.getHeight()/2 - (group.getHeight()/2));
    
    stage.addActor(group);
    

    I hope that helps you