如何设置Android游戏的计时器?

时间:2022-06-28 02:48:59

I want to programm a game for Android.

我想为Android编程游戏。

Play principle: The user should have a short time to choose the correct answer.

播放原则:用户应该有很短的时间来选择正确的答案。

My problem is the combination between the input (choosing the answer) and the time (countdown). I tried to run a thread, but the thread isn't stoppable. So the user gives the answer, the next activity will be shown and after a while (when the "timer" becomes 0) the activity will be shown again.

我的问题是输入(选择答案)和时间(倒计时)之间的组合。我试图运行一个线程,但线程不可停止。因此,用户给出答案,将显示下一个活动,并且一段时间后(当“计时器”变为0时)将再次显示活动。

  • How could I implement this in a correct way?
  • 我怎么能以正确的方式实现这个?

  • Should I use Thread, Handler, CountDownTimer ?
  • 我应该使用Thread,Handler,CountDownTimer吗?

3 个解决方案

#1


2  

You can keep a running timer using this on init:

您可以在init上使用此命令保持运行的计时器:

Timer updateTimer = new Timer("update");
updateTimer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    updateGUI();
        }
    }, 0, 1000);
long startTime = System.currentTimeMillis();

Then in a Thread:

然后在一个线程中:

//Clock update
currentTime = System.currentTimeMillis() - startTime;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
clock.setText(sdf.format(currentTime));
clock.invalidate();

You could stop the Thread with a boolean inside or outside as you please?

您可以根据需要在内部或外部使用布尔值来停止Thread吗?

#2


0  

Okay, I don't know the specific libraries to use, and I haven't done any thread programming myself, but I would think of the program as a state machine. I hope it helps :/

好吧,我不知道要使用的特定库,我自己没有做过任何线程编程,但我认为该程序是一个状态机。我希望它有所帮助:/

Set up a boolean userHasAnswered = false.

设置布尔userHasAnswered = false。

Set up a specialized listener (e.g. touch listener for some button) for answers. If the listener gets an appropriate response from the user, set userHasAnswered = true.

设置专门的监听器(例如,某个按钮的触摸监听器)以获得答案。如果侦听器从用户获得适当的响应,请设置userHasAnswered = true。

When question loads up, start the thread which counts down.

当问题加载时,启动倒计时的线程。

If user fails to give ans when the time reaches zero, just call loadNextQuestion().

如果用户在时间到零时未能给出ans,则只需调用loadNextQuestion()。

In your thread, do periodic checks every few units of time to see if userHasAnswered == true, and if it is true, then call updateScore() and loadNextQuestion().

在你的线程中,每隔几个单位进行定期检查以查看userHasAnswered == true,如果为true,则调用updateScore()和loadNextQuestion()。

#3


0  

You may want to have a look at alarm manager

您可能想看一下警报管理器

#1


2  

You can keep a running timer using this on init:

您可以在init上使用此命令保持运行的计时器:

Timer updateTimer = new Timer("update");
updateTimer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    updateGUI();
        }
    }, 0, 1000);
long startTime = System.currentTimeMillis();

Then in a Thread:

然后在一个线程中:

//Clock update
currentTime = System.currentTimeMillis() - startTime;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
clock.setText(sdf.format(currentTime));
clock.invalidate();

You could stop the Thread with a boolean inside or outside as you please?

您可以根据需要在内部或外部使用布尔值来停止Thread吗?

#2


0  

Okay, I don't know the specific libraries to use, and I haven't done any thread programming myself, but I would think of the program as a state machine. I hope it helps :/

好吧,我不知道要使用的特定库,我自己没有做过任何线程编程,但我认为该程序是一个状态机。我希望它有所帮助:/

Set up a boolean userHasAnswered = false.

设置布尔userHasAnswered = false。

Set up a specialized listener (e.g. touch listener for some button) for answers. If the listener gets an appropriate response from the user, set userHasAnswered = true.

设置专门的监听器(例如,某个按钮的触摸监听器)以获得答案。如果侦听器从用户获得适当的响应,请设置userHasAnswered = true。

When question loads up, start the thread which counts down.

当问题加载时,启动倒计时的线程。

If user fails to give ans when the time reaches zero, just call loadNextQuestion().

如果用户在时间到零时未能给出ans,则只需调用loadNextQuestion()。

In your thread, do periodic checks every few units of time to see if userHasAnswered == true, and if it is true, then call updateScore() and loadNextQuestion().

在你的线程中,每隔几个单位进行定期检查以查看userHasAnswered == true,如果为true,则调用updateScore()和loadNextQuestion()。

#3


0  

You may want to have a look at alarm manager

您可能想看一下警报管理器