libgdx - 当我调用setScreen()设置新屏幕时,屏幕在新屏幕和旧屏幕之间闪烁

时间:2014-09-24 16:25:41

标签: libgdx

我有一个名为MenuScreen的屏幕,当按下新的游戏按钮时,它会设置一个新的屏幕GameScreen。发生这种情况时,屏幕在MenuScreen和GameScreen之间闪烁,就像隐藏了MenuScreen一样。以下是MenuScreen,GameScreen和输入处理器的代码。

注意:现在,GameScreen只是为了显示紫色背景。

MenuScreen

public class MenuScreen implements Screen {

    private TextureRegion title;
    private TextureRegion newgameup;
    private TextureRegion loadgameup;
    private NewGameButton newgame;
    private SpriteBatch batcher;
    float screenWidth = Gdx.graphics.getWidth();
    float screenHeight = Gdx.graphics.getHeight();
    private float height;
    private float width;
    private MyGame game;
    private OrthographicCamera cam;
    private InputHandler inputHandler;
    private float timer;

    public MenuScreen(MyGame game) {
        this.game = game;
    }

    @Override
    public void show() {
        Gdx.app.log("MenuScreen", "show method");
        title = AssetLoader.title;
        newgameup = AssetLoader.newgameup;
        loadgameup = AssetLoader.loadgamedwn;
        width = 800; // Gdx.graphics.getWidth();
        height = 480; // Gdx.graphics.getHeight();
        cam = new OrthographicCamera();
        cam.setToOrtho(false, 800, 480);
        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
        inputHandler = new InputHandler(game, width, height);
        Gdx.input.setInputProcessor(inputHandler);

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batcher.begin();
        batcher.draw(title, (width / 2 - title.getRegionWidth() / 2),
                (height / 2 + 25), 288, 52);
        inputHandler.getNewgame().draw(batcher);
        inputHandler.getLoadgame().draw(batcher);
        batcher.end();

        if (game.isGameScreen()) {
            timer += delta;
            if (timer > 1) {
                game.setScreen(new GameScreen(game));
            }
        }
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
    }

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

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

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

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

GameScreen

public class GameScreen implements Screen {
    private MyGame game;
    private SpriteBatch batcher;

    public GameScreen(MyGame game) {
        this.game = game;
    }

    @Override
    public void show() {
        Gdx.app.log("GameScreen", "show method");
        Gdx.gl.glClearColor(255/255.0f, 50/255.0f, 255/255.0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batcher = new SpriteBatch();
    }

    @Override
    public void render(float delta) {
        batcher.begin();
        batcher.end();
    }
}

InputHandler

public class InputHandler implements InputProcessor{
    private NewGameButton newgame;
    private SimpleButton loadgame;
    private float width, height;
    private MyGame game;


    public InputHandler(MyGame game, float width, float height) {
        Gdx.app.log("InputHandler", "constructor");
        this.game = game;
        this.width = width;
        this.height = height;

        newgame = new NewGameButton((width / 2 - AssetLoader.newgameup.getRegionWidth() / 2), 
                (height / 2 - 40), (float) 200, (float) + 35, AssetLoader.newgameup, AssetLoader.newgamedwn);

        loadgame = new SimpleButton((width / 2 - AssetLoader.loadgameup.getRegionWidth() / 2), 
                (height / 2 - 100), (float) 186, (float) + 37, AssetLoader.loadgameup, AssetLoader.loadgamedwn);
    }

    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Gdx.app.log("InputHandler", "touch down"+screenX + "," +screenY);
        Gdx.app.log("InputHandler", "activescreen: "+game.activescreen);
        if (game.isMenuScreen()) {

            newgame.isTouchDown(screenX, screenY);
        }

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (game.isMenuScreen()) {
            if (newgame.isTouchUp(screenX, screenY)) {
                game.gamescreen();
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }

    public NewGameButton getNewgame() {
        return newgame;
    }

    public SimpleButton getLoadgame() {
        return loadgame;
    }

    public void setNewgame(NewGameButton newgame) {
        this.newgame = newgame;
    }

    public void setLoadgame(SimpleButton loadgame) {
        this.loadgame = loadgame;
    }
}

1 个答案:

答案 0 :(得分:2)

所以非常沮丧之后,我意识到我在发布问题后不久就犯了一个简单的错误:我在show()中调用了我的Gdx.gl.glClear()和Gdx.gl.Clear()方法。比render()。