在LibGDX中更新Camera

在LibGDX中更新Camera

本文介绍了在LibGDX中更新Camera.Update之后无法绘制Sprite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Libgdx Developement的新手,并且正在从事类似Candy Crush之类的游戏.我有一个背景板,在该板上有许多颜色不同的硬币,用户可以在其中拖动和玩耍.除一个问题外,所有代码都工作正常,其中一个问题是我创建了一个逻辑,使用户达到多个点并进行了升级,这时我想显示一个弹出窗口,说"Level Up + NumberOfLevel".我创建了一个结合了背景图片和字体的弹出窗口.但是我无法画出来,因为我在 camera.update()之后调用它.

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

我延迟使用了布尔型 stopGameObjects ,因此可以在render()方法上绘制精灵.

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

这是我的render()方法的代码:

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

render方法中的这段代码用于绘制弹出窗口:

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

如果将这段代码放在camera.update()上方,则会在屏幕截图中显示输出.弹出窗口位于硬币的后面.为了将其显示在硬币上方,我需要将其放在for循环之后绘制 gameobject ,但是它不起作用.请坚持我的建议,因为我坚持了3天.

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.

推荐答案

这是因为您要在文本上方绘制游戏的其余部分.

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

要解决此问题,您可以在绘制所有其他精灵后将文本的绘制向下移动到.请记住再次拨打setProjectionMatrix().

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

这篇关于在LibGDX中更新Camera.Update之后无法绘制Sprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 18:09