DaxHR
DaxHR

Reputation: 683

Libgdx changing scenes doesn't destroy previous scene

So in scene A I have few buttons, one of them leads to scene B. When I open scene B, Table elements are not placed correctly and buttons from previous scene are invisible, but if I tap on position of that button from previous scene it opens a scene that it opens on scene A.

I hope this makes sense to you.

I'm thinking the problem is in dispose method.

Here's the code of my scene B:

public class Postavke implements Screen {

    Main main = null;

    // KONSTRUKTOR ZA MJENJANJE SCENE
    public Postavke(Main gm){
        main = gm;
    }

    Stage postavkeStage = null;
    private Image pozadina = null;


    @Override
    public void show() {
        postavkeStage = new Stage(new StretchViewport(1920, 1080));
        pozadina = new Image(new Texture(Gdx.files.internal("pozadinaPostavke.png")));
        postavkeStage.addActor(pozadina);
        postavkeButtoni();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        postavkeStage.draw();
        postavkeStage.act();
    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        postavkeStage.dispose();

    }

    private void postavkeButtoni(){
        Table postavkeButtoni = new Table();
        postavkeButtoni.setFillParent(true);

        final Image zvukButton = new Image(new Texture(Gdx.files.internal("postavkemutebutton.png")));
        final Image homeButton = new Image(new Texture(Gdx.files.internal("homebutton.png")));
        final Image mailButton = new Image(new Texture(Gdx.files.internal("postavkegmailbutton.png")));
        final Image instagramButton = new Image(new Texture(Gdx.files.internal("postavkeinstagrambutton.png")));
        final Image zaslugeButton = new Image(new Texture(Gdx.files.internal("postavkezaslugebutton.png")));



        zvukButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                zvukButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                zvukButton.addAction(Actions.scaleTo(1, 1,.1f));
                main.setScreen(new MainMenu(main));
            }
        });

        homeButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                homeButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                homeButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        mailButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                mailButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                mailButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        instagramButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                instagramButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                instagramButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        zaslugeButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                zaslugeButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                zaslugeButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        postavkeButtoni.add(homeButton).expand().top().left().pad(20);
       // postavkeButtoni.row();
        postavkeButtoni.add(zvukButton);
        //postavkeButtoni.row();
        HorizontalGroup drustveneMrezeIZasluge = new HorizontalGroup();
        postavkeButtoni.addActor(mailButton);
        postavkeButtoni.addActor(instagramButton);
        postavkeButtoni.addActor(zaslugeButton);

        postavkeStage.addActor(postavkeButtoni);

    }
}

Why isn't it placing things properly and destroying scene A?

Scene A:

public class MainMenu implements Screen {

    private Stage mainMenu = null;
    private SpriteBatch batch = null;
    private Main game = null;

    // MORAŠ IMAT OVAJ KONSTRUKTOR ZA MJENJANJE SCENE
    public MainMenu(Main gm){
        game = gm;
    }

    @Override
    public void show() {
        // KREIRANJE MAIN MENU EKRANA
        mainMenu = new Stage(new StretchViewport(1920, 1080));
        // POSTAVLJANJE INPUT PROCESORA
        Gdx.input.setInputProcessor(mainMenu);
        // IMAGE ZA POZADINU
        Image pozadina = new Image(new Texture(Gdx.files.internal("pozadinaProba.png")));
        // DODAVANJE POZADINE NA STAGE
        mainMenu.addActor(pozadina);

        createButtons();

    }

    private void createButtons(){
        Table buttonsTable = new Table();
        buttonsTable.setFillParent(true);
        final Image playButton = new Image(new Texture(Gdx.files.internal("playbutton.png")));
        final Image rateButton = new Image(new Texture(Gdx.files.internal("rateusbutton.png")));
        final Image exitButton = new Image(new Texture(Gdx.files.internal("exitbutton.png")));
        final Image howToPlayButton = new Image(new Texture(Gdx.files.internal("howtoplaybutton.png")));
        final Image ostaleAplikacijeButton = new Image(new Texture(Gdx.files.internal("ostaleaplikacijebutton.png")));
        final Image instagramButton = new Image(new Texture(Gdx.files.internal("instagrambutton.png")));
        final Image zaslugeButton = new Image(new Texture(Gdx.files.internal("zaslugebutton.png")));

        playButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                playButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                playButton.addAction(Actions.scaleTo(1, 1,.1f));
                game.setScreen(new GameScreen(game));
            }
        });

        rateButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                rateButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                rateButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        exitButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                exitButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                exitButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        howToPlayButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                howToPlayButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                howToPlayButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });

        ostaleAplikacijeButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                ostaleAplikacijeButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                ostaleAplikacijeButton.addAction(Actions.scaleTo(1, 1,.1f));
                Gdx.net.openURI("https://play.google.com/store/search?q=pub%3A%22Dario%20Rade%C4%8Di%C4%87%22&c=apps");
            }
        });

        instagramButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                instagramButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                instagramButton.addAction(Actions.scaleTo(1, 1,.1f));
                Gdx.net.openURI("https://www.instagram.com/darioradecic/");
            }
        });


        zaslugeButton.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
                zaslugeButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
                return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button){
                zaslugeButton.addAction(Actions.scaleTo(1, 1,.1f));
            }
        });



        HorizontalGroup horizontalGroup = new HorizontalGroup();
        horizontalGroup.padTop(340);
        horizontalGroup.addActor(playButton);
        horizontalGroup.addActor(rateButton);
        horizontalGroup.addActor(exitButton);
        //buttonsTable.add(playButton).spaceRight(40);
        //buttonsTable.add(rateButton).spaceRight(40);
        //buttonsTable.add(exitButton);
        buttonsTable.add(horizontalGroup);
        buttonsTable.row();
        buttonsTable.add(howToPlayButton).padTop(50);
        buttonsTable.row();
        HorizontalGroup donjaGrupa = new HorizontalGroup().bottom().padLeft(Gdx.graphics.getWidth()-550);
        donjaGrupa.addActor(ostaleAplikacijeButton);
        donjaGrupa.addActor(instagramButton);
        donjaGrupa.addActor(zaslugeButton);
        buttonsTable.add(donjaGrupa);
        mainMenu.addActor(buttonsTable);

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        mainMenu.draw();
        mainMenu.act();

    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        this.mainMenu.dispose();
        this.dispose();

    }
}

Upvotes: 0

Views: 290

Answers (1)

iibrahimbakr
iibrahimbakr

Reputation: 1134

The simple code as you can see:

1- Create MainGame extends Game:

public class Main extends Game {

    @Override
    public void create() {
        setScreen(new SceneA());
    }
    @Override
    public void dispose() {
    // your SceneA and SceneB here will dispose here after you call setScreen(new ____());
        getScreen().dispose(); 
        System.out.println("\n" + getScreen().getClass().getSimpleName() + " disopsed");
        System.out.println("=========================================================");
    }
}

2- Craete SceneA implements Screen:

public class SceneA implements Screen {

    private Stage stage;

    @Override
    public void show() {
        System.out.println("\nshow SceneA");

        stage = new Stage(new StretchViewport(1920, 1080));

        Image button = new Image(new Texture("____.png"));
        button.addListener(new ClickListener() {
            public void clicked(InputEvent event, float x, float y) {
                ((Game) Gdx.app.getApplicationListener()).dispose();
                ((Game) Gdx.app.getApplicationListener()).setScreen(new SceneB());
            }
        });

        stage.addActor(button);

        // don't forget to call this to be able to handle stage inputs
        Gdx.input.setInputProcessor(stage);
    }

    // ........ here remaining methods (resize(), resume(), pause(), hide())
    @Override
    public void dispose() {
        stage.dispose();
        System.out.println("\ndispose SceneA");
    }
}

3- Create SceneB implements Screen:

public class SceneB implements Screen {

    private Stage stage;

    @Override
    public void show() {
        System.out.println("\nshow SceneB");

        stage = new Stage(new StretchViewport(1920, 1080));

        Image button = new Image(new Texture("____.png"));
        button.addListener(new ClickListener() {
            public void clicked(InputEvent event, float x, float y) {
                ((Game) Gdx.app.getApplicationListener()).dispose();
                ((Game) Gdx.app.getApplicationListener()).setScreen(new SceneA());
            }
        });

        stage.addActor(button);

        // don't forget to call this to be able to handle stage inputs
        Gdx.input.setInputProcessor(stage);
    }

    // ........ here remaining methods (resize(), resume(), pause(), hide())
    @Override
    public void dispose() {
        stage.dispose();
        System.out.println("\ndispose SceneB");
    }
}

Upvotes: 1

Related Questions