我是Libgdx Developement的新手,我正在開發一款類似於Candy Crush的遊戲。我有一個背景板,在該板上方有許多具有不同顏色的硬幣,用戶可以拖動和玩。除了一個問題之外,所有代碼都可以正常工作,其中我創建了一個邏輯,用戶可以實現多個點和級別,此時我想要顯示一個彈出窗口,說明Level Up + NumberOfLevel。我創建了一個背景圖片和字體的組合。但我無法畫出,因爲我在camera.update()之後調用它。無法在LibGDX 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);
}
如果我把上面的相機這個代碼。 update()我得到輸出顯示在屏幕截圖中。彈出在硬幣後面。爲了顯示它上面的硬幣,我需要把它放在for循環繪製gameobject之後,但它不起作用。請給出你的建議,因爲我堅持了3天。
非常感謝。你是一個拯救生命的人:) –
我真的很感謝@Vallentin的幫助。我需要一個更多的青睞。我也試圖動畫這個彈出:https://stackoverflow.com/questions/43465424/unable-to-animate-sprite-in-libgdx-using-tween 如果可以的話,請給你的建議。 –