更改屏幕时,LibGDX屏幕重叠

时间:2014-11-19 00:40:58

标签: java libgdx screen overlapping

嗨,我是libGDX的新手,我只是创建一个简单的游戏应用程序。我有一个主菜单屏幕和我的游戏的关卡屏幕。我已将启动屏幕初始化为主菜单。当我点击“播放”时在主菜单屏幕中的按钮,我想将屏幕切换到水平选择屏幕,但会发生的是它确实改变了屏幕但旧的屏幕也在后面。这是当我点击播放按钮时发生的事情:http://prntscr.com/57teri

游戏课程:

public class BallJump extends Game {

MainMenu menu;
LevelSelect levelSelect;


@Override
public void create () {
    menu = new MainMenu(this);
    levelSelect = new LevelSelect(this);

    setScreen(menu);

}

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

}

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

}

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


}


}

主菜单屏幕:

public class MainMenu implements Screen{

BallJump game;
Stage stage;
GameButton playButton, exitButton;
Texture title;
Sprite test;
SpriteBatch batch;
OrthographicCamera camera;

public MainMenu(BallJump game) { 
    this.game = game;
}



@Override
public void render(float delta) {
    Gdx.gl20.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    System.out.println("main menu");

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

    batch.begin();
    test.draw(batch);
    batch.end();


}

@Override
public void show() {
    camera = new OrthographicCamera();
    title = new Texture(Gdx.files.internal("images/title.png"));
    test = new Sprite(title);


    batch = new SpriteBatch();

    stage = new Stage();

    test.setPosition(Gdx.graphics.getWidth()/7f, Gdx.graphics.getHeight()/1.75f);



    playButton = new GameButton("PLAY");
    playButton.setPosition(Gdx.graphics.getWidth()/2.5f, Gdx.graphics.getHeight()/2);
    playButton.setHeight(50f);
    playButton.setWidth(150f);
    playButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            game.setScreen(game.levelSelect);
            dispose();

                return true;
        }});

    stage.addActor(playButton);

    exitButton = new GameButton("EXIT");
    exitButton.setPosition(Gdx.graphics.getWidth()/2.5f, Gdx.graphics.getHeight()/3);
    exitButton.setHeight(50f);
    exitButton.setWidth(150f);
    exitButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {

                return true;
        }});

    stage.addActor(exitButton);

    Gdx.input.setInputProcessor(stage);
}



@Override
public void hide() {


}

@Override
public void resize(int width, int height) {
     camera.viewportWidth = width;
     camera.viewportHeight = height;
     camera.update();

}

@Override
public void dispose() {
    batch.dispose();
    stage.clear();
    stage.dispose();
    title.dispose();



}

级别选择屏幕:

public class LevelSelect implements Screen{

BallJump game;
Stage stage;
TextButton l1, buttonExit;
Texture title;
Sprite test;
SpriteBatch batch;
OrthographicCamera camera;

public LevelSelect(BallJump game) { 
    this.game = game;
}





@Override
public void show() {
    batch = new SpriteBatch();
    stage = new Stage();

    title = new Texture(Gdx.files.internal("images/level_select.png"));
    test = new Sprite(title);
    test.setPosition(Gdx.graphics.getWidth()/7f, Gdx.graphics.getHeight()/1.75f);



    stage.addActor(l1);

}





@Override
public void render(float delta) {
    stage.act();
    stage.draw();

    batch.begin();
    test.draw(batch);
    batch.end();
    System.out.println("level select");

}

1 个答案:

答案 0 :(得分:3)

您的LevelSelect屏幕需要在绘图前清除缓冲区。如果不这样做,菜单中最后一个渲染的帧将始终位于缓冲区上。

@Override
public void render(float delta) {
   Gdx.gl20.glClearColor(0, 0, 0, 1);
   Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

   ...
}