LibGDX - 如何清除屏幕

时间:2013-08-03 16:48:01

标签: java android libgdx

我正在尝试显示2个不同的屏幕,当用户触摸屏幕时会改变。到目前为止,屏幕下方的代码发生了变化,但文本只是保持重叠和堆积。在切换之前,我需要在屏幕上处理所有内容。

2个相似页面中的一个(仅2上的文字不同)

package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Label;

public class MainMenu implements Screen {
    OrthographicCamera camera;
    SpriteBatch batch;
    Screens game;
    BitmapFont font;

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

    @Override
    public void dispose() {
        batch.dispose();
        font.dispose();
    }

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

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void render(float delta) {
        CharSequence str = "Main Menu";
        batch = new SpriteBatch();
        font = new BitmapFont();

         batch.begin();
         font.draw(batch, str, 200, 200);
         batch.end();

        if (Gdx.input.justTouched()) // use your own criterion here
            game.setScreen(game.anotherScreen);
    }

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

    }

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

    }
}

Screens.java

package com.me.mygdxgame;

import com.badlogic.gdx.Game;

public class Screens extends Game {


    MainMenu mainMenuScreen;
    AnotherScreen anotherScreen;


   @Override
    public void create() {
            mainMenuScreen = new MainMenu(this);
            anotherScreen = new AnotherScreen(this);
            setScreen(mainMenuScreen);              
    }
}

1 个答案:

答案 0 :(得分:5)

将渲染功能更改为:

@Override
public void render(float delta) {

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);      //clears the buffer 

    CharSequence str = "Main Menu";
    batch = new SpriteBatch();
    font = new BitmapFont();

     batch.begin();
     font.draw(batch, str, 200, 200);
     batch.end();

    if (Gdx.input.justTouched()) // use your own criterion here
        game.setScreen(game.anotherScreen);
}