Scrollpane not working, help needed

Anything libgdx related goes here!

Scrollpane not working, help needed

Postby rsanterre » Sun Jan 12, 2020 5:04 pm

Hey, working on a game and I need a scroll bar to go through an list of Labels, I have provided the code below, some of it has been omitted for brevity.
I'm trying too put the scrollpane code in render() instead of create(), is that possible?


Code: Select all

 @Override
    public void create() {
        skin = new Skin(Gdx.files.internal("uiskin.json"));
        stage = new Stage(new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera));

@Override
    public void render() {
        batch.setProjectionMatrix(camera.projection);
        batch.setTransformMatrix(camera.view);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        if (currentScreen == Screen.MAIN_MENU) {
CODE OMMITED
        } else if (currentScreen == Screen.MONSTER_COLLECTION) {

}

            Table tableInScrollPaneAtMonsterCollection = new Table(skin);
            tableInScrollPaneAtMonsterCollection.add("Blah");
            tableInScrollPaneAtMonsterCollection.row();
            tableInScrollPaneAtMonsterCollection.add("blahblah");
            tableInScrollPaneAtMonsterCollection.row();
            tableInScrollPaneAtMonsterCollection.add("blahblahblah");
            ScrollPane scrollPaneAtMonsterCollection = new ScrollPane(tableInScrollPaneAtMonsterCollection, skin);
            scrollPaneAtMonsterCollection.setWidth(160);
            scrollPaneAtMonsterCollection.setHeight(40);
            scrollPaneAtMonsterCollection.setY(40);

            stage.addActor(buttonAtMonsterCollectionTooMainMenu);
            stage.addActor(scrollPaneAtMonsterCollection);
            Gdx.input.setInputProcessor(stage);
            stage.act(Gdx.graphics.getDeltaTime());


            stage.draw();
}


Thanks,
rsanterre
 
Posts: 1
Joined: Sun Jan 12, 2020 4:50 pm

Re: Scrollpane not working, help needed

Postby shatterblast » Mon Jan 13, 2020 5:52 am

In certain situations, it's actually preferable to put your UI code in the render() method. You would want to declare a boolean variable first somewhere in the beginning of the class.

For instance...
Code: Select all
private boolean firstLoad = true;


It's important to not put this boolean variable in a method. In your render() method, you would want something like....
Code: Select all
render() {
    if ( firstLoad ) {
        //Such and such code.
    }

    //Whatever other normal code.
}


In many circumstances but not all, you will be able to move stuff from your create() method into your render() method this way. It will load your stuff as if from the create() method.

The reason I follow this pattern is because different platforms behave in different ways. For the most part, APK stuff should go into the create() method and a few other things. UI stuff doesn't matter much in this regard, so it actually benefits. The create() method partially acts as a Constructor for certain things, which is why it exists.
shatterblast
 
Posts: 653
Joined: Sun Jul 06, 2014 1:14 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 1 guest