为游戏重复倒数计时器

时间:2014-05-20 02:12:33

标签: android

我创造了一个游戏,用户每回合都有X秒的时间进行移动,因此我需要显示剩余的秒数并控制游戏。

如果用户在倒计时结束前进行了移动,倒计时将被取消并再次作为新转弯开始。如果倒计时在用户移动之前结束,则游戏结束。

重要提示:每秒钟(计时器嘀嗒事件)都会播放声音,这不会影响计时器间隔。

我该怎么做?我应该使用Timer,CountDownTimer还是别的什么?

2 个答案:

答案 0 :(得分:1)

您必须使用CountDown Timer作为您的要求........

在这里你可以控制计时器调用以下代码......

if (!timerHasStarted) { 
           countDownTimer.start(); 
           timerHasStarted = true; 
           startB.setText("STOP"); 
          } else { 
           countDownTimer.cancel(); 
           timerHasStarted = false; 
           startB.setText("RESTART"); 
          }

请点击以下链接..

http://androidbite.blogspot.in/2012/11/android-count-down-timer-example.html

答案 1 :(得分:0)

您可以使用CountDownTimer:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
        //Play your audio here
     }

     public void onFinish() {
         //Trigger the game end action
     }
  }.start();

致电cancel()以停止计时器。

参考:CountDownTimer