javalibgdxscreens

Libgdx when changing screens freezes


I can't find the solution and I'm looking for few hours now.. when i change screens everything just freezes. I dont know what is wrong. Everything should be fine. Is it because of memory loss or loop stops somewhere?

MY CODE:

public class Main extends Game {

    MainMenuOne mainmenu;
    public GameScreen gamescreen;
    private static boolean change;
    @Override
    public void create() {
        System.out.println("ZBGame Created!");
        AssetLoader.load();
        setScreen(new MainMenuOne(null));

    }


    @Override
    public void dispose() {

        super.dispose();
        //AssetLoader.dispose();
    }

}







public class MainMenuOne implements ApplicationListener, Screen {
    private GameWorld world;
    private MenuRenderer renderer;
    private float runTime;
    private Player player;
    private MoveButton movebutton;
    private bUp bup;

    private static int buttonsize;

    public static Stage stage; //** stage holds the Button **//
    static BitmapFont font; //** same as that used in Tut 7 **//
    public static int seed;
    private TextureAtlas buttonsAtlas; //** image of buttons **//
    private Skin buttonSkin; //** images are used as skins of the button **//
    private TextButton button; //** the button - the only actor in program **//


    private OrthographicCamera cam;

    // This is the constructor, not the class declaration
    public MainMenuOne(final Main main) {

        final Random r = new Random();
        buttonsAtlas = new TextureAtlas("ui/buttons.pack"); //** button atlas image **// 
        buttonSkin = new Skin();
        buttonSkin.addRegions(buttonsAtlas); //** skins for on and off **//
        font = new BitmapFont(Gdx.files.internal("ui/default.fnt"),false); //** font **//

        stage = new Stage();        //** window is stage **//
        stage.clear();
        Gdx.input.setInputProcessor(stage); //** stage is responsive **//

        TextButtonStyle style = new TextButtonStyle(); //** Button properties **//
        style.up = buttonSkin.getDrawable("button.up.9.png");
        style.down = buttonSkin.getDrawable("button.down.9.png");
        style.font = font;


        buttonsize = Gdx.graphics.getWidth()/6;
        float screenWidth = Gdx.graphics.getWidth();
        float screenHeight = Gdx.graphics.getHeight();
        float gameWidth = Gdx.graphics.getWidth()/2;
        float gameHeight = screenHeight / (screenWidth / 136);


        world = new GameWorld();
        renderer = new MenuRenderer(world,(int) gameHeight);
        player = new Player(world, (int) gameHeight);

        bup = new bUp (world, (int) gameHeight);

        Table table=new Table();
        table.setSize(gameWidth,gameHeight);




        movebutton = new MoveButton(world, (int) gameHeight);
        Gdx.input.setInputProcessor(new InputHandler(world));
        Gdx.input.setInputProcessor(stage);



        button = new TextButton( "S", style); //** Button text and style **//
        button.setPosition(screenWidth-buttonsize*2, 0); //** Button location **//
        button.setHeight(buttonsize); //** Button Height **//
        button.setWidth(buttonsize); //** Button Width **//





        button.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                main.setScreen(new GameScreen(null));
                dispose();  

                   return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                bUp.noJudam();
            }
        });




        stage.addActor(button);

    }

    @Override
    public void render(float delta) {

        //System.out.println(Gdx.graphics.getWidth()/2);
        stage.act();
        player.update(delta);
        movebutton.update(delta);
        runTime += delta;
        bup.update(delta);
        world.update(delta);
        renderer.render(runTime);
    }

    @Override
    public void resize(int width, int height) {
        System.out.println("GameScreen - resizing");
    }

    @Override
    public void show() {
        System.out.println("GameScreen - show called");

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {
        System.out.println("GameScreen - pause called");
    }

    @Override
    public void resume() {
        System.out.println("GameScreen - resume called");
    }

    @Override
    public void dispose() {
        //this.dispose();
        buttonSkin.dispose();
        buttonsAtlas.dispose();
        font.dispose();
        stage.dispose();

    }

    @Override
    public void create() {
        // TODO Auto-generated method stub

    }

    @Override
    public void render() {
        // TODO Auto-generated method stub

    }


}

Solution

  • you pass null as the param here:

    setScreen(new MainMenuOne(null));
    

    That is a bad idea, since the constructor expects a Game object as the parameter. Try this instead:

    setScreen(new MainMenuOne(this));
    

    The same for this screen btw:

    main.setScreen(new GameScreen(null));
    

    You should pass again a reference to your 'main' game:

    main.setScreen(new GameScreen(main));