libgdx:切换屏幕时图像不会消失

时间:2018-04-26 14:31:35

标签: java android libgdx

此刻我有2个屏幕:MainScreen(这是游戏的主菜单)和GameScreen。我有一个“按钮”(它是一张图片),当用户点击它时,MainScreen会切换到GameScreen

问题是当出现第二个屏幕(按钮和主标题)时,MainScreen的所有图像都不会消失。我已经做了好几天了,我不知道还能做些什么。

游戏的主要类别:

public class MyGame extends Game {

    @Override
    public void create () {
        Sounds.load();
        Texts.load();
        Buttons.load();

        setScreen(new MainScreen(this));
    }

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

MainScreen:

public class MainScreen implements Screen {

    private MyGame game;

    private Stage stage;
    private OrthographicCamera camera;
    private Viewport viewport;

    public static Image tituloPrincipal, botonPlay;



    public MainScreen(final MyGame game) {

        this.game = game;

        //stage
        camera = new OrthographicCamera(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT);
        viewport = new StretchViewport(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT, camera);
        stage = new Stage(viewport);

        //background
        stage.addActor(new Image(Sprites.mainBackground));

        //Title of the game
        tituloPrincipal = new Image();
        tituloPrincipal.setPosition(Settings.SCREEN_WIDTH  * 1 / 6, Settings.SCREEN_HEIGHT * 4 / 12);
        tituloPrincipal.setDrawable(new TextureRegionDrawable(new TextureRegion(Texts.tituloPrincipal)));
        tituloPrincipal.setSize(Texts.tituloPrincipal.getWidth()/3,
                Texts.tituloPrincipal.getHeight()/3);
        stage.addActor(tituloPrincipal);

        //Play Button
        botonPlay = new Image();
        botonPlay.setPosition(Settings.SCREEN_WIDTH  * 4 / 10, Settings.SCREEN_HEIGHT / 16);
        botonPlay.setDrawable(new TextureRegionDrawable(new TextureRegion(Buttons.playbutton)));
        botonPlay.setSize(Buttons.playbutton.getWidth()/16,
                Buttons.playbutton.getHeight()/18);

        stage.addActor(botonPlay);


        //Song
        Sounds.musicMainScreen.play();


    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        botonPlay.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.setScreen(new GameScreen(game));
                dispose();
            }
        });


            stage.draw();
            stage.act(delta);


    }

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

    }

    @Override
    public void pause () {

    }

    @Override
    public void resume () {

    }

    @Override
    public void hide () {

    }

    @Override
    public void dispose () {

    }
}

GameScreen:

public class GameScreen implements Screen {
    private Game game;
    private Stage stage;

    public GameScreen(Game game) {
        this.game = game;
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        deleteMainScreenSources();


        //song
        Sounds.musicGameScreen.play();

    }

    public void deleteMainScreenSources() {
        Sounds.musicMainScreen.stop();
    }

    @Override
    public void show() {
    }

    @Override
    public void render(float delta) {


    }

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

    }

    @Override
    public void pause () {

    }

    @Override
    public void resume () {

    }

    @Override
    public void hide () {

    }

    @Override
    public void dispose () {

    }
}

按钮类:

public class Buttons {
    public static Texture playbutton;


    public static void load() {
        playbutton = new Texture(Gdx.files.internal("playbutton.png"));
        playbutton.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

    }

    public static void dispose() {
        playbutton.dispose();
    }

}

标题的类就像按钮的类。

1 个答案:

答案 0 :(得分:0)

您的render方法是绘制屏幕的方法。如果你不知道,它是一种游戏循环,它会不间断地执行(我相信默认大约60 /秒)。因此,所有绘图和重绘都会在这里进行。通常,您首先要清除屏幕,然后在屏幕上绘制其他图像。 Libgdx提供与opengl的接口以清除屏幕。在我的情况下,我总是在render方法的第一行清除屏幕并在其上绘制其他内容,以确保屏幕上的陈旧内容消失。在GameScreen,您的{ {1}}方法为空,因此您之前的屏幕尚未消失。我相信在GameScreen中添加清晰并绘制新阶段将解决您的问题。

在GameScreen中使用这样的代码进行渲染并尝试,

render

另外,出于同样的原因,我会尽量避免在'render`方法中创建任何新对象。菜单屏幕中的addListener方法可以放在它的构造函数中。这样你就不会在每个游戏循环执行中创建匿名监听器。在你当前的场景中,这可能不会受到那么多的影响,但是如果你在游戏循环中以这种方式创建一些重物或一些精灵,那么可能会拖累你的游戏性能。