簡體   English   中英

如何在固定時間段內在android中顯示“游戲結束”圖像並在指定間隔后用戶點擊屏幕時重新啟動游戲?

[英]how to display the “Game over” image in android for a fixed amount of time and restart the game when user taps the screen after a specified interval?

我通過觀看視頻tutorials.i對其進行了克隆,制作了“ Flappy bird”游戲的副本,這樣當鳥掉下或與管子碰撞時,屏幕上會出現游戲結束消息,而當玩家點擊屏幕時,游戲會重新開始。

問題在於,當用戶未能及時敲打鳥並與管碰撞時,立即出現游戲畫面,並且用戶碰巧在屏幕上點擊游戲,從而導致游戲重新開始。

這使用戶無法看到分數。我已經嘗試使用Thread.sleep()。下面是代碼

    (gameState == 2)
    {
        batch.draw(gameOver,Gdx.graphics.getWidth()/2-gameOver.getWidth()/2,Gdx.graphics.getHeight()/2-gameOver.getHeight()/2);

        try
        {
            Thread.sleep(2000);
        }
        catch(InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }

        if (Gdx.input.justTouched()) {
            gameState = 1;
            startGame();
            score =0;
            scoringTube = 0;
            velocity = 0;
        }
    }

使用此代碼的問題是,即使游戲結束圖像也被延遲並且先前的問題仍在發生,但是現在延遲了。救命。

我真的不建議使用Thread.sleep ; 相反,您可以嘗試使用一個布爾值,該值在游戲結束后會更改,並在這種情況下阻止方法執行。 將其與例如在固定延遲后將其重置的計時器結合使用,您應該可以解決問題。

計時器的示例用法:

new java.util.Timer().schedule( 
    new java.util.TimerTask() {
        @Override
        public void run() {
            //execute code here (change boolean state)
        }
    }, 
    yourDelayHere 
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM