2014-12-01 59 views
0

作爲標題說我試圖在我的遊戲中顯示一個從0開始的計時器(理想情況下,我希望它位於屏幕的左上角)LibGDX - 試圖在我的遊戲中顯示一個可見的計時器

我有一個計時器這裏的邏輯:

public class Timer { 
SpriteBatch batch; 
private BitmapFont font; 
private float deltaTime = 0; 
CharSequence str; 

public Timer() { 
    font = new BitmapFont(); 
    batch = new SpriteBatch(); 
} 
public void drawTime() { 
    deltaTime += Gdx.graphics.getDeltaTime(); 
    str = Float.toString(deltaTime); 
    font.draw(batch, str, 0, 0); 
} 

} 

我稱之爲我的主類定時器(遊戲)在渲染()方法,像這樣:

public void render() { 

    player.update(); 
    platform1.update(); 
    platform2.update(); 

    batch.begin(); 

    Gdx.gl.glClearColor(135/255f, 206/255f, 235/255f, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    flag.drawS(batch); 
    flag.draw(batch); 

    player.draw(batch); 

    platform1.draw(batch); 
    platform2.draw(batch); 

    timer.drawTime(); 
    batch.end(); 
} 
} 

我得到th e錯誤「SpriteBatch begin必須在繪製前調用」,所以我嘗試在render()的不同位置移動timer.drawTime()方法,但仍然沒有運氣。

任何人都知道什麼可能是錯的?任何幫助高度讚賞:)

回答

3

你不應該創建SpriteBatch()裏面你的計時器對象。 SpriteBatch應該被創建一次並被多個元素用來繪製自己。您的定時器draw()方法應該看起來更像是這樣的:

public void drawTime(SpriteBatch batch) { 
    deltaTime += Gdx.graphics.getDeltaTime(); 
    str = Float.toString(deltaTime); 
    font.draw(batch, str, 0, 0); 
} 

您遇到由你一個不同的SpriteBatch對象那麼獲取drawTime()使用的一個上調用的事實引起特定的錯誤。

+0

啊,這確實有道理(特別是當我看着render()中其他對象的繪製方法時)。進行了更改,但屏幕上仍然沒有定時器 – DeuceDeuce 2014-12-01 15:52:43

+0

它可能會從屏幕上或背景顏色中脫落。另外,我建議從font.draw開始(batch,「TEST」,0,0); – atok 2014-12-01 15:54:54

+0

我看到了,我現在將其位置設置爲(100,00),並可以在屏幕左下角看到它。我一直認爲在android(0,0)是屏幕的左上角,雖然很奇怪。感謝您的幫助 – DeuceDeuce 2014-12-01 16:17:57