0
\$\begingroup\$

I'm writing a little something in libgdx to learn more about the platform. Currently, I have a simple generated world consists of tiles, which are just blocks of texture drawn to the screen. It's a simple setup, but I'm wondering how would I go about switching to a different "scene". For instance, just imagine a player, walking around in a grass field, enters a cave, and bam, he is in the cave. The world behind him has transitioned from a grass field to a cave (essentially, from one generated world to another).

I'm not very sure how libgdx handles scenes, so if anyone can share some tips, I'd appreciate it.

NOTE: I know this is a very broad question; right now I'm not asking for a specific solution but rather some tips to help me approach the idea I'm going after.

\$\endgroup\$
0
\$\begingroup\$

Do you have everything in the main game class? If so you can move each scene into its separate scene which each implements the Screen class.

For example

import com.badlogic.gdx.Screen;

public abstract class AbstractScene implements Screen
{

protected Game game;
protected Stage mainStage;
protected Stage uiStage;
public final int viewWidth;
public final int viewHeight;
protected boolean paused;

public AbstractScene(Game g, int viewWidth, int viewHeight)
{
    game = g;
    this.viewWidth = viewWidth;
    this.viewHeight = viewHeight;
    mainStage = new Stage(new FitViewport(viewWidth, viewHeight));
    uiStage = new Stage(new FitViewport(viewWidth, viewHeight));

    create();
}

public abstract void create();
public abstract void update(float dt);

@Override
public void render(float dt)
{
    uiStage.act(dt);
    if (!paused)
    {
        mainStage.act(dt);
        update(dt);
    }
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    mainStage.draw();
    uiStage.draw();
}
}

Now all you need to do is call the following function whenever you need to swap scenes.

game.setScreen(new ScreenClass());
| improve this answer | |
\$\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.