1
\$\begingroup\$

I'm having trouble showing a simple custom VerticalGroup.

Here's my SideBar class:

public class SideBar extends VerticalGroup {
    final GameManager gameManager = GameManager.instance;
    final Image nextBackground;
    final Image nextImage;
    final Image powerImage;
    final Image powerUp1Background;
    final Image powerUp2Background;
    final Image powerUp3Background;


    public SideBar() {
        super();
        nextBackground = new Image(gameManager.getTexture("bar.next.png"));
        addActor(nextBackground);
        nextImage = new Image(gameManager.getTexture("label.next.png"));
        addActor(nextImage);
        powerImage = new Image(gameManager.getTexture("label.power.png"));
        addActor(powerImage);
        powerUp1Background = new Image(gameManager.getTexture("powerup.bg.png"));
        addActor(powerUp1Background);
        powerUp2Background = new Image(gameManager.getTexture("powerup.bg.png"));
        addActor(powerUp2Background);
        powerUp3Background = new Image(gameManager.getTexture("powerup.bg.png"));
        addActor(powerUp3Background);
    }
}

And here's where I add it to my main Stage:

    TopBar topBar = new TopBar();
    topBar.setY(DonutsGame.WORLD_HEIGHT - topBar.getHeight());
    stage.addActor(topBar);

    SideBar sideBar = new SideBar();
    Gdx.app.log(TAG, "side bar - x,y: " + sideBar.getX() + "," + sideBar.getY() + ", w,h: " + sideBar.getWidth() + "," + sideBar.getHeight());
    stage.addActor(sideBar);

My TopBar class extends Group and is displaying correctly, so I'm not sure what I'm doing wrong with SideBar that's causing it to not show.

The log output shows that the position, and width/height are all 0, which seems incorrect to me according to the vertical group's documentation. Which states that the width/height should be based on the pref. width/height of the children (height based on sum).

I've tried calling validate and layout with no success.

\$\endgroup\$
1
  • \$\begingroup\$ If I manually set the width and height, then the group shows, and I just re-read the documentation. It actually says preferred width/height are based on children. However, the examples I can find don't say anything about having to set the width and height. \$\endgroup\$ – Townsfolk Jun 21 '16 at 22:45
1
\$\begingroup\$

It seems that if the VerticalGroup is added to another layout, then it works. Looking at the code, this is due to VerticalGroup also being a Layout, and so it's prefWidth/Height are used instead of the getWidth/Height methods.

I got around this by manually setting the size to the pref. size.

setSize(getPrefWidth(), getPrefHeight());

Note, that you don't need to do this if you're putting the VerticalGroup in another WidgetGroup like Table or ScrollPane since they use prefWidth/Height anyway.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.