Search code examples
androidlibgdx

How to change gamestates in LibGdx using stacks


I am making a game in android like flappy bird and I am able to go from the menu state to the playState but when the game is over and when I try to get the menu state back it just gives me the background or sometimes a white screen.

public class FlappyDemo extends ApplicationAdapter {//ApplicatoonListener

public static final int width=480;
public static final int height=800;
public static final String title="Flappy Bird";
public GameStateManager gsm;
/*private*/public SpriteBatch spriteBatch;
public void create () {
    spriteBatch=new SpriteBatch();
    gsm=new GameStateManager();
    gsm.push(new MenuState(gsm));

    Gdx.gl.glClearColor(1,1,1,1);



}

@Override
public void render () {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.render(spriteBatch);


}

}

public abstract class State {
OrthographicCamera cam;
Vector3 mouse;
GameStateManager gsm;
public State(GameStateManager gsm){
    this.gsm=gsm;
    mouse=new Vector3();

    cam = new OrthographicCamera();
}
public abstract void handleInput();
public abstract void update(float dt);
public abstract void render(SpriteBatch sb);
public abstract void dispose();

}

public class GameStateManager {

private Stack<State> states;

public GameStateManager(){
    states=new Stack<State>();
}
public void push(State state){
    states.push(state);

    }
public void pop(){
    states.pop();
}

public void set(State state){
    states.pop().dispose();
    states.push(state);


}


public void update(float dt){
    states.peek().update(dt);

}
public void render(SpriteBatch sb){
    states.peek().render(sb);
}

}

public class MenuState extends State {

Texture background;
Texture playBtn;
public MenuState(GameStateManager gsm) {
    super(gsm);
    background=new Texture("bg.png");
    playBtn=new Texture("buttonflappy.jpg");
}

@Override
public void handleInput() {
    if(Gdx.input.isTouched()) {
        gsm.push(new PlayState(gsm));
    }

}

@Override
public void update(float dt) {
    handleInput();
}

@Override
public void render(SpriteBatch sb) {
    sb.begin();
    sb.draw(background,0,0, 1100,1800);
    sb.draw(playBtn,1100/2, 1800/2);
    sb.end();
}

@Override
public void dispose() {
    playBtn.dispose();
    background.dispose();

}

}

so first I call the push method and pass it the MENUSTATE and than in the menustate I call the push method and pass it the PlayState to start the game and in the PLayState I have got the gameplay, cameras,viewports. Now when I say that if the bird hits the tube or the ground the menustate should be called again all I get is a green screen which is the background of the playstate.

  if (tube.collide(bird.getBirdBound())) {

                gsm.set(new MenuState(gsm));

            }
            if (bird.getPosition().y <= ground.getHeight() + offset) {

                gsm.set(new MenuState(gsm));

            }

this is the calling method to the gamestate manager when the game ends and the menustate should be called. So can anyone tell me how can I get the menustate back instead of getting the background color i.e green. Really appreciate if anyone could help.


Solution

  • I finally got it the problem was that I was not changing the camera position. SO what I did was that in the menustate I used the camera and set its position cam.setToOrtho(false,1100,1800) the position where I had drawn the background and updated the camera so now when in the playstate I poped the current state and went back to to the menustate my camera was pointing at the menustate insted of the green color.