Libgdx改变场景并不会破坏以前的场景

时间:2016-05-02 13:22:49

标签: java android libgdx

因此在场景AI中只有几个按钮,其中一个导致场景B.当我打开场景B时,表格元素放置不正确,前一个场景中的按钮不可见,但如果我点击前一个按钮的位置场景它打开一个在场景A上打开的场景。

我希望这对你有用。

我认为问题出在处理方法中。

这是我的场景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);

    }
}

为什么没有正确放置东西并摧毁场景A?

场景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();

    }
}

1 个答案:

答案 0 :(得分:1)

您可以看到简单的代码:

1-创建MainGame扩展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实施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-创建SceneB实施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");
    }
}