Search code examples
androidlibgdxscrollpane

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?


Solution

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