Harikrishnan
Harikrishnan

Reputation: 8063

libGDX Scrollpane not scrolling for single Label

I am adding "How to Play" instructions to a game which I have created using libGDX. The instructions scroll need to be shown only inside a fixed area of the screen, not as a full screen scroll. I have created a stage and added some background and other actors, and then some labels which are the title(outside the scroller) and the instructions text(the only thing that need to be scrolled). It also should not be scrolling horizontally, just vertically. So I set the width of the Label and wrapped the text. In short I need full control of the position and size of the scroller, it shouldn't be full screen. So I wrote the following code to add the instruction scroller:

private void addHelpContent()
{
    int width = (int) (0.85*alertBounds.width);
    int height = scrollStartY - scrollEndY;
    int x = Constants.APP_WIDTH/2 - width/2;

    BitmapFont font = Model.getInstance().createFont(Constants.ROCK_NORMAL_FONT_FILE_PATH, 20);
    Label.LabelStyle labelStyle = new Label.LabelStyle();
    labelStyle.font = font;
    labelStyle.fontColor = Color.valueOf("A07E10FF");
    Label label = new Label(getHelpString(), labelStyle);
    label.setWidth(width);
    label.setWrap(true);
    label.pack();
    label.setFillParent(true);
    ScrollPane scrollPane = new ScrollPane(label);
    scrollPane.setBounds(x, scrollEndY, width, height); //This should be the bounds of the scroller and the scrollable content need to be inside this
    scrollPane.layout();
    scrollPane.setTouchable(Touchable.enabled);
    addActor(scrollPane);
}

But now I am facing two problems:

  1. I am not able to scroll the scrollpane. The label is cut off right at the bounds making the Scrollpane useless.
  2. The other actors except the Labels doesn't appear at first, they will appear only after I scroll the scrollpane(and it won't scroll). This problem is not there if I comment off the code to add the scroll pane. So I am sure it is related to the scrollpane itself.

Can anyone please help me solve this?

Upvotes: 1

Views: 976

Answers (2)

Harikrishnan
Harikrishnan

Reputation: 8063

I just forgot to add the act() method in the stage class. Adding it solved the issue.

Upvotes: 2

Tekkerue
Tekkerue

Reputation: 1517

The scrollpanel is a little tricky to get it to work. From my understanding, you cannot set the size of the scrollpane as it will automatically size itself to the same size as the label.

The trick is to add your scrollpane to a table. Then when the size of the label is larger than the size of the table the scrollpane will allow the label to scroll within the table. To make sure it doesn’t scroll horizontal, set the width of the table to the same width as the label. You also shouldn’t have to set the bounds of the scrollpane, let it size itself to the label.

Then instead of adding the scrollpane, add the table that contains the scrollpane.

addActor(table);

Upvotes: 1

Related Questions