Vlad Zhabinsky
Vlad Zhabinsky

Reputation: 33

Can't switch screen [Libgdx]

i'm new to libgdx and i can't figure out what i do wrong. In my game I just want to switch between 2 screens (first is menuScreen, second is gameScreen). I have created these two screens and one game class, it all looks okay. But when i call my method setScreen(new GameScreen()) nothing happens. Also i should say, that i used screen and game classes in my previous project and all was ok, when i compare my current code to previous one i do not see any differences, so it is very curiously.

Here is my MenuScreen class:

MenuScreen implements Screen {

private SpriteBatch batch ;
private Texture texture;
private float timePassed;


public MenuScreen() {
    timePassed = 0;
    batch = new SpriteBatch();
    texture = new Texture("texture.png");

@Override
public void render (float delta) {
    timePassed += Gdx.graphics.getDeltaTime();
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (timePassed > 5) {GameClass.getInstance().setScreen(new GameScreen());} //it looks strange, but it's to check if all works properly
    batch.begin();
    batch.draw(texture, 0, 0);
    batch.end();
}

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

@Override
public void hide() {

}

@Override
public void resume() {

}

@Override
public void pause() {

}

@Override
public void resize(int width, int height) {

}

@Override
public void show() {

}}

Here is my GameScreen class:

GameScreen implements Screen {

private SpriteBatch batch2 ;
private Texture texture2;


public MenuScreen() {
    batch2 = new SpriteBatch();
    texture2 = new Texture("texture2.png");
}
@Override
public void render (float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch2.begin();
    batch2.draw(texture2, 0, 0);
    batch2.end();
}

@Override
public void dispose() {
    batch2.dispose();
    texture2.dispose();
}

@Override
public void hide() {

}

@Override
public void resume() {

}

@Override
public void pause() {

}

@Override
public void resize(int width, int height) {

}

@Override
public void show() {

}}

And Game class:

public class ClassGame extends Game {
public static ClassGame getInstance(){
    ClassGame instance = new ClassGame();
    return instance;
}
@Override
public Screen getScreen() {
    return super.getScreen();
}

@Override
public void setScreen(Screen screen) {
    super.setScreen(screen);
}
@Override
public void resize(int width, int height) {
    super.resize(width, height);
}

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

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

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

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

public ClassGame() {
    super();
}

@Override
public void create() {
    MenuScreen menuScreen = new MenuScreen();
    setScreen(menuScreen);
}}

Upvotes: 2

Views: 455

Answers (2)

dilz
dilz

Reputation: 147

This is how I did for the same problem, i had three classes for two screen to switch between them. Here I am switching between second and third using a image actor as button.I also used two constructors in each class, one of them constructors with no arguments, below is my code hope you will get it. My main class code:

public class First extends Game{
private Game game;
public Main(){ game=this; }
public void create() {
    game.setScreen(new Second(game)); }}

Below is my first screen class code

public class Second implements Screen{
private Game second;
public Second(Game second){this.second=second;}
public Second(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image secondButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show(){
    myGroup.addActor(secondButton);
    secondButton.addListener(new InputListener(){
    second.setScreen(new Third(second)); });}
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myStage.act(Gdx.graphics.getDeltaTime());
     myStage.draw(); }
public void resize(int width, int height) {}
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}

And my next screen class code

 public class Third implements Screen{
private Game third;
public Third(Game third){
    this.third=third;}
public Third(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image thirdButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show() {
    myGroup.addActor(thirdButton);
    thirdButton.addListener(new InputListener(){
        third.setScreen(new Third(third));});}
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myStage.act(Gdx.graphics.getDeltaTime());
    myStage.draw();}
public void resize(int width, int height) {    }
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}

Upvotes: 1

Vlad Zhabinsky
Vlad Zhabinsky

Reputation: 33

I got it, guys. In my previous project i declared instance field outside of getInstance(). Like this :

    public static GameClass instance = new GameClass();

    public static GameClass getInstance(){
          return instance;
}

But i still need your help. I don't know how this small thing wasn't letting me switch screens.

Upvotes: 0

Related Questions