我是Libgdx Developement的新手,我正在开发像Candy Crush一样的游戏。我有一块背景板,在那块板子上面有许多不同颜色的硬币,用户可以拖放。所有代码都工作正常,除了一个问题,我已经创建了一个逻辑,用户可以实现多个点和级别,此时我想显示一个弹出文件,说明Level Up + NumberOfLevel。我创建了弹出的背景图像和字体的组合。但我无法绘制,因为我在 camera.update()之后调用它。
这是我整个逻辑的代码:
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()方法上绘制精灵。
这是我的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方法中的代码用于绘制弹出窗口:
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循环绘制游戏对象之后将其放置,但它无法正常工作。请给出你的建议,因为我坚持了3天。
答案 0 :(得分:3)
这是因为你在文本之上绘制了游戏的其余部分。
要解决此问题,您可以在绘制所有其他精灵之后将文本的绘图向下移动。请务必再次致电setProjectionMatrix()
。
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);
}