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