简体   繁体   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?

I made clone of ""Flappy bird" game by watching video tutorials.i programmed it so that when the bird falls or collides with the tubes a game over message appears on the screen and the game restarts when the player taps on the screen. 我通过观看视频tutorials.i对其进行了克隆,制作了“ Flappy bird”游戏的副本,这样当鸟掉下或与管子碰撞时,屏幕上会出现游戏结束消息,而当玩家点击屏幕时,游戏会重新开始。

The problem is that when the user fails to tap the bird in time and it collides with the tube,the game over screen appears immediately and the user happens to tap on the game over screen which results in restarting of the game. 问题在于,当用户未能及时敲打鸟并与管碰撞时,立即出现游戏画面,并且用户碰巧在屏幕上点击游戏,从而导致游戏重新开始。

This makes the user unable to see the score.I have already tried using Thread.sleep().Following is the code 这使用户无法看到分数。我已经尝试使用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;
        }
    }

With this code the problem is that even the gameover image is being delayed and the previous problem is still occuring but now with a delay.I basically need a way so that justTouched method becomes inactive for a while when the game over screen is there.Please help. 使用此代码的问题是,即使游戏结束图像也被延迟并且先前的问题仍在发生,但是现在延迟了。救命。

I really wouldn't recommend using Thread.sleep ; 我真的不建议使用Thread.sleep ; instead, you could try to use a boolean that is changed once the game ended, and prevent the method from executing in that case. 相反,您可以尝试使用一个布尔值,该值在游戏结束后会更改,并在这种情况下阻止方法执行。 Combine that with eg a Timer that resets it after a fixed delay, and you should have the solution to your problem. 将其与例如在固定延迟后将其重置的计时器结合使用,您应该可以解决问题。

Example Usage for the timer: 计时器的示例用法:

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