升级Libgdx版本后,舞台不绘图

时间:2017-04-21 10:20:00

标签: android libgdx sprite stage

我正在制作游戏,我正试图在游戏结束时画一个舞台。 当我使用旧版本的Libgdx时,这工作正常。当我更新libGdx版本时,我遇到了以下问题。 我正在为 gameOver 使用相同的背景变量 gamePlayBG ,我将其用于整个游戏并且每次都重置它。目前但舞台不是画画。只有gamePlayBG正在绘制。这是我的代码:

private void gameOver() {


    gameOverStage = new Stage();

    Gdx.input.setInputProcessor(gameOverStage);

    //gameOverStage.setCamera(camera2);

    if (isBackgroundMusicPlaying()) {
        game.freePlaySound.stop();
    }


    StretchViewport viewport = new StretchViewport(480, 853);
    gameOverStage.setViewport(viewport);

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    gamePlayBG = new Texture(Gdx.files.internal("data/main_bg.png"));



    TextureRegionDrawable gameOverBG = new TextureRegionDrawable();

    gameOverBG.setRegion(new TextureRegion(new 
   Texture(Gdx.files.internal("data/bg_gameover.png"))));


    Button mainMenuButton = new Button();
    ButtonStyle mainMenuStyle = new ButtonStyle();
    mainMenuStyle.up = new TextureRegionDrawable(new TextureRegion(new 
  Texture(Gdx.files.internal("data/home.png"))));
    mainMenuButton.setStyle(mainMenuStyle);
    mainMenuButton.addListener(new InputListener() {


        public boolean touchDown(InputEvent event, float x, float y, int 
 pointer, int num) {


            if (game.soundBoolean) {
                game.objectRemovedSound.play();
            }

            game.goToMainMenu();

            return false;
        }


    });


    TextButton.TextButtonStyle playAgainStyle = new 
TextButton.TextButtonStyle();
    playAgainStyle.up = new TextureRegionDrawable(new TextureRegion(new 
Texture(Gdx.files.internal("data/green.png"))));
    playAgainStyle.font = game.scoreFonts;
    TextButton playAgainButton = new TextButton("Play Again", 
playAgainStyle);
    playAgainButton.getLabel().setFontScale(0.70f);
    playAgainButton.addListener(new InputListener() {


        public boolean touchDown(InputEvent event, float x, float y, int pointer, int num) {

            restart();

            if (game.soundBoolean) {
                game.buttonSound.play();
            }

            return false;
        }


    });


    Button awardsButton = new Button();
    ButtonStyle awardsButtonStyle = new ButtonStyle();
    awardsButtonStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/awards.png"))));
    awardsButton.setStyle(awardsButtonStyle);
    awardsButton.addListener(new InputListener() {


        public boolean touchDown(InputEvent event, float x, float y, int pointer, int num) {


            if (game.soundBoolean) {
                game.objectRemovedSound.play();
            }


            /**
             * Put navigation to awards screen here.
             */

            return false;
        }


    });


    /**
     * Time up label settings
     */
    LabelStyle timeUpLabelStyle = new LabelStyle();
    timeUpLabelStyle.font = game.greenShadowFont;

    Label timeUpLabel = new Label("Time up!", timeUpLabelStyle);
    timeUpLabel.setFontScale(1.7f);


    /**
     * Your Score up label settings
     */
    LabelStyle yourScoreLabelStyle = new LabelStyle();
    yourScoreLabelStyle.font = game.scoreFonts;

    Label yourScoreLabel = new Label("YOUR SCORE IS:", yourScoreLabelStyle);
    yourScoreLabel.setFontScale(1.0f);


    /**
     * Setting current score text
     */
    LabelStyle currentScoreLabelStyle = new LabelStyle();
    currentScoreLabelStyle.font = game.gameTopFonts;

    Label currentScoreLabel = new Label("" + score, currentScoreLabelStyle);
    currentScoreLabel.setFontScale(1.3f);


    /**
     * Time up label settings
     */
    LabelStyle yourHighScoreLabelStyle = new LabelStyle();
    yourHighScoreLabelStyle.font = game.scoreFonts;

    Label yourHighScoreLabel = new Label("YOUR HIGH SCORE:", yourHighScoreLabelStyle);
    yourHighScoreLabel.setFontScale(0.8f);


    /**
     * Setting high score text
     */
    LabelStyle highScoreLabelStyle = new LabelStyle();

    highScoreLabelStyle.font = game.gameTopFonts;
    int highScore = game.prefs.getInteger("highScore", score);

    Label highScoreLabel = new Label("" + highScore, highScoreLabelStyle);
    highScoreLabel.setFontScale(1.1f);


    /**
     * Creating tables for buttons, background and labels
     */
    Table bgTable = new Table();
    Table buttonsTable = new Table();

    Table timeUpTable = new Table();
    Table yourScoreTable = new Table();
    Table yourHighScoreTable = new Table();
    Table currentScoreTable = new Table();
    Table highScoreTable = new Table();


    Image gameOverBGImg = new Image(gameOverBG);
    bgTable.add(gameOverBGImg);


    bgTable.setFillParent(true);


    /**
     * Adding score labels to table
     */
    timeUpTable.add(timeUpLabel);
    yourScoreTable.add(yourScoreLabel);
    highScoreTable.add(highScoreLabel);
    currentScoreTable.add(currentScoreLabel);
    yourHighScoreTable.add(yourHighScoreLabel);

    /**
     * Assigning alignment/positions to buttons
     */
    buttonsTable.add(awardsButton).padRight(20);
    buttonsTable.add(playAgainButton).padRight(20);
    buttonsTable.add(mainMenuButton);

    //gameOverStage.addActor(background);

    gameOverStage.addActor(bgTable);
    gameOverStage.addActor(buttonsTable);
    gameOverStage.addActor(currentScoreTable);
    gameOverStage.addActor(highScoreTable);
    gameOverStage.addActor(yourScoreTable);
    gameOverStage.addActor(yourHighScoreTable);
    gameOverStage.addActor(timeUpTable);

    buttonsTable.setPosition(260, 175);
    timeUpTable.setPosition(260, 580);
    yourScoreTable.setPosition(260, 480);
    currentScoreTable.setPosition(260, 420);
    yourHighScoreTable.setPosition(260, 325);
    highScoreTable.setPosition(260, 275);

    gameOver = true;

    if (score >= highScore) {

        game.prefs.putInteger("highScore", score);
        game.prefs.flush();


    }

}

这是我的渲染方法,当时间为零时调用gameOver()方法:

public void render(float delta) {
    // TODO Auto-generated method stub

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    game.batch.begin();

    game.batch.setProjectionMatrix(camera2.combined);

    game.batch.draw(gamePlayBG, 0, 0);


    if (gameOver != true) {
        //Log.d("render------------", "render: "+ delta);


        progressBar.draw(game.batch, 1);

        progressBar.setPosition(115, 700);
        progressBar.setHeight(15);
        progressBar.setWidth(270);


    camera.update();

    game.batch.setProjectionMatrix(camera.combined);


    if (lineHud.size > 1) {
        for (Sprite sprite : this.lineHud) {

            sprite.draw(game.batch);
        }
    }


    for (int i = 0; i < gameObjects.size; i++) {
        gameObjects.get(i).draw(game.batch);
    }


    for (Sprite sprite : this.scoreHud) {

        sprite.draw(game.batch);


    }


    if (stopGameObjects) {

        game.batch.setProjectionMatrix(camera2.combined);

        levelupButton.draw(game.batch, 1);
       /* levelUp.draw(game.batch);

        game.levelUpFont.drawMultiLine(game.batch, "LEVEL " + (baseLevel + 1),
                (gamePlayBG.getWidth() / 2) - 55,
                (gamePlayBG.getHeight() / 2) + 35);

        game.levelUpFont.drawMultiLine(game.batch, "KEEP GOING",
                (gamePlayBG.getWidth() / 2) - 90,
                gamePlayBG.getHeight() / 2);*/


        /*game.levelUpFont.drawMultiLine(game.batch,"KEEP GOING",
                (gamePlayBG.getWidth() / 2) - 50,
                (gamePlayBG.getHeight() / 2) + 50);*/


    }


    game.tweenManager.update(delta);

    game.batch.end();


    update(delta);


    game.batch.setProjectionMatrix(camera2.combined);

    if (gameOver == true) {


        this.gameOverStage.act();
        this.gameOverStage.draw();


    }


}


public void update(float deltaTime) {

    if (gameOver == false) {
        progressBar.setValue(gameTime);

        gameTime -= deltaTime;

    }

    if (gameTime <= 0) {


        if (gameOver == false) {
            gameOver();

        }


    }


}

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

覆盖resize / Screen的{​​{1}}方法并更新您的舞台视口。

Game

答案 1 :(得分:0)

在你的gameOver方法中,替换

StretchViewport viewport = new StretchViewport(480, 853);
gameOverStage.setViewport(viewport);

Viewport viewport = new ExtendViewport(480, 853);
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
gameOverStage.setViewport(viewport);