Unable to Draw Sprite after Camera.Update in LibGD

2019-06-09 07:55发布

I am new to Libgdx Developement and I am working on a game same like Candy Crush. I have a background board, above that board there are number of coins with different colors where users can drag and play. All code is working fine except one issue, where I have created a logic where user achieves a number of points and levels up, at that moment I want to display a pop up saying Level Up + NumberOfLevel. I have created that pop up with combination of a background image and font. But I am unable to draw that because I am calling it after camera.update() .

Here is my code for the whole logic :

private void initLevelLogic() {

    if (baseLevel == 0) {
        baseTime = 180;
        basePoint = 400;

        gameTime = baseTime;

        progressBar = new ProgressBar(0, baseTime, 1, false, progressBarStyle);
        progressBar.setValue(baseTime);

        baseScoreToMinus = 0;

        baseLevel++;
    } else {
        Log.d("Runnable", "RUNNABLE CALLED");

        initLevelUpAnimation(baseLevel);

        Timer.schedule(new Task() {
            @Override
            public void run() {
                baseTime -= 10;

                basePoint = basePoint + (100 * baseLevel);

                /**
                 * Game time will be used to calculate in render() method which is going to be in float.
                 */
                gameTime = baseTime;

                progressBar = new ProgressBar(0, baseTime, 1, false, progressBarStyle);
                progressBar.setValue(baseTime);

                baseScoreToMinus = 0;

                baseLevel++;
                delayTime = 0;


                stopGameObjects = false;
            }
        }, 5.0f);
    }
}

private void initLevelUpAnimation(int level) {
    levelUp = new Sprite(levelUpBG);
    levelUp.setSize(250, 250);
    levelUp.setPosition(gamePlayBG.getWidth() / 2 - levelUp.getWidth() / 2,
            gamePlayBG.getHeight() / 2 - levelUp.getHeight() / 2);

    stopGameObjects = true;
}

I have used a boolean, stopGameObjects with a delay so I can draw sprite on render() method.

Here is my code of render() method :

public void render(float delta) {
    Gdx.gl.glClear(GL10.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);

        game.gameTopFonts.draw(game.batch, "Level " + baseLevel + " - ", 140, 750);
        game.gameTopFonts.draw(game.batch, (basePoint - baseScoreToMinus) + " Points to Go", 225, 750);

       if (score == 0) {
            game.scoreFonts.draw(game.batch, score + "", 300, 810);
        } else if (score < 100) {
            game.scoreFonts.draw(game.batch, score + "", 280, 810);
        } else if (score < 1000) {
            game.scoreFonts.draw(game.batch, score + "", 260, 810);
        } else {
            game.scoreFonts.draw(game.batch, score + "", 245, 810);
        }
    }

    //Writing this here will give output shown in the screenshot
    if (stopGameObjects) {
        levelUp.draw(game.batch);

        game.levelUpFont.drawMultiLine(game.batch, "Level " + baseLevel + 1 + "\n" + "KEEP GOING", gamePlayBG.getWidth() / 2,
                gamePlayBG.getHeight() / 2);

    }

    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);
    }
}

This code in render method is for drawing the pop up :

if (stopGameObjects) { 
    levelUp.draw(game.batch);
    game.levelUpFont.drawMultiLine(game.batch,
            "Level " + baseLevel + 1 + "\n" + "KEEP GOING",
            gamePlayBG.getWidth() / 2,
            gamePlayBG.getHeight() / 2); 
}

If I put this code above camera.update() I get output shown in the screenshot. The pop up is behind the coins. In order to show it above coins I need to put it after the for loop drawing the gameobject , but it is not working. Please give your suggetions as I am stuck with this for 3 days.

enter image description here

1条回答
We Are One
2楼-- · 2019-06-09 08:22

It's because you're drawing the rest of the game on top of the text.

To fix this you could move the drawing of the text down to after having drawn all other sprites. Remember to call setProjectionMatrix() again.

if (stopGameObjects) {
    game.batch.setProjectionMatrix(camera2.combined);

    levelUp.draw(game.batch);
    game.levelUpFont.drawMultiLine(game.batch,
            "Level " + baseLevel + 1 + "\n" + "KEEP GOING",
            gamePlayBG.getWidth() / 2,
            gamePlayBG.getHeight() / 2); 
}
查看更多
登录 后发表回答