L. F
L. F

Reputation: 103

How to set screen in libgdx properly

I'm currently making a 2d infinite runner esque game that has a release date of Halloween. I've nearly finished the game itself and I've just added a main menu and am currently in the process of making a pause menu. I'm having a big issue with setting the screens though. Setting the screen from my main menu into the game works perfectly fine but when I set the screen back to the main menu a few weird things happen:

  1. Nothing in the spriteBatch renders, everything is just a black box (MainMenuScreen)
  2. If I choose to go back to the game I find that not everything is reset like when the screen is initially set to GameScreen

Here's a video of the issue

Here's the code for how I switch screens:

From when the game is first opened to main menu:

public class RadiationPigeon extends Game {

public static final float PPM = 100;

public static SpriteBatch batch;


@Override
public void create () {
    batch = new SpriteBatch();
    setScreen(new MainMenuScreen(this));
}

@Override
public void render () {
    super.render();
}


@Override
public void dispose () {
    batch.dispose();
}}

From MainMenuScreen to GameScreen:

private RadiationPigeon radiationPigeon;

public MainMenuScreen(RadiationPigeon radiationPigeon){
    this.radiationPigeon = radiationPigeon;
}

playButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {

            return true;

        }

        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {

            radiationPigeon.setScreen(new GameScreen(radiationPigeon)); 
//I give GameScreen radiationpigeon so that the screen can be set back to MainMenu

        }
    });

From GameScreen to MainMenu:

private RadiationPigeon radiationPigeon;

public GameScreen(RadiationPigeon radiationPigeon){

    world.setContactListener(new ContactListener());
    Timer timer = new Timer();
    timer.schedule(new BatAttackChanceCounter(), 0, 1000);
    this.radiationPigeon = radiationPigeon;
}

pauseButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {

            return true;

        }

        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {

            //paused = true;
            radiationPigeon.setScreen(new MainMenuScreen(radiationPigeon));

        }
    });

Just for reference, here's my render method of both screens:

MainMenuScreen:

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act();
    RadiationPigeon.batch.begin();
    stage.draw();
    RadiationPigeon.batch.end();

}

GameScreen:

@Override
public void render(float delta) {

    update(delta);

    Gdx.gl.glClearColor(0, 0,0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    RadiationPigeon.batch.setProjectionMatrix(pigeoncam.combined);

    RadiationPigeon.batch.begin();

    //RadiationPigeon.batch.draw(pausedScreen, pigeoncam.position.x - (250 / RadiationPigeon.PPM), pigeoncam.position.y - pigeoncam.viewportHeight / 2, 5, 5);


    RadiationPigeon.batch.end();


    b2dr.render(world, pigeoncam.combined);

    stage.draw();

    hud.stage.draw();
}

Upvotes: 3

Views: 1635

Answers (1)

L. F
L. F

Reputation: 103

After lots of trial and error I finally resolved my issue. There was nothing wrong with how I set my screens (for anyone having trouble with setting screens the code I provided works). The problem was with the actual buttons and images. I had made some of them static because I needed to use the same buttonstyles and fonts in GameScreen. I thought that would save memory, however, once they were accessed outside the screen in which they were created (MainMenuScreen) they would lose all their values essentially making them empty without making them null.

A lack of understanding on how buttons worked is what caused the issue

EDIT:

Turns out a lack of understanding on how static variables work was the real issue. On android when you minimize an android app the java cycle doesn't stop and as such the items are "kept alive"

Upvotes: 2

Related Questions