juliano.net
juliano.net

Reputation: 8177

Repeat countdown timer for game

I'm creating a game where the user will have X seconds to make his move every turn, so I need to show the amount of seconds remaining and control the game.

If the user makes his move before countdown is complete, the countdown is cancelled and started again as a new turn. And if the countdown ends before the user makes his move, the game is ended.

Important: each second (timer tick event) a sound is played and this shouldn't affect the timer interval.

How can I do this? Should I use a Timer, a CountDownTimer or something else?

Upvotes: 0

Views: 581

Answers (2)

Devganiya Hitesh
Devganiya Hitesh

Reputation: 1207

You have to use CountDown Timer as your requirement........

here you can control timer calling Below code...

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

follow below link..

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

Upvotes: 1

hungr
hungr

Reputation: 2096

You may use CountDownTimer:

new CountDownTimer(30000, 1000) {

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

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

call cancel() to stop the timer.

Reference: CountDownTimer

Upvotes: 0

Related Questions