如何在Android游戏中制定时间限制(限制)?

时间:2013-07-20 20:31:09

标签: android time

我制作了一个应用程序,用户需要在时间过去时完成某项任务。我的想法是:

向用户显示任务 - >开始计算秒数 - >如果任务在某些秒内没有得到解决,应用程序会写:"你失败了"。

我脑子里只有一个解决方案,因为直到现在我还没有遇到过 - 显示使用的任务并启动计数秒和等待的线程,如:

sleep(1000);
secondsCounter++;
if (secondsCounter => LIMIT){
    write("You failed!");
}

但有些事情告诉我,这不是解决问题的正确方法。还有其他(更好)的方式吗?

1 个答案:

答案 0 :(得分:1)

使用处理程序:

// set this to true if user succeeds before time runs out.
boolean userSucceeded = false;

new Handler().postDelayed(new Runnable() {
    public void run() {
        if (!userSucceeded) {
            // write("you failed")
        }
    }
}, 1000L); // 1 second