android - countdown with decreasing time

473 views Asked by At

In my game the user gets a point when he clicks a button within five seconds. Now I want the timer to decrease the time with every point the user gets - so e.g. with zero points the user has five seconds, and when he has one point he gets only 4.5 seconds to click it again and get the second point. Do I solve it with a for loop?

public class GameScreen extends Activity {
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Button count = (Button) findViewById(R.id.button1);
        text = (TextView) this.findViewById(R.id.textView3);
        tvscore = (TextView) findViewById(R.id.score);

        timer();
    }

    public void gameover() {
        Intent intent = new Intent(this, GameOverScreen.class);
        startActivity(intent);
    }

    public void onClick (View view) {
        score++;
        tvscore.setText(String.valueOf(score));
        timer();
    }

    public void timer(){
        new CountDownTimer(5000, 10) {
            public void onTick(long millisUntilFinished) {
                text.setText(""+String.format("%02d:%03d",
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
                        ));
                if(animationRunning) {
                    cancel();
                }
            }
            public void onFinish() {
                text.setText("Too slow.");
                gameover();
            }
        }.start();
    }
}
1

There are 1 answers

2
kristyna On BEST ANSWER

What about doing this:

public void timer(float time) {
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}

public void onClick (View view) {
    score++;
    tvscore.setText(String.valueOf(score));
    timer(Math.max(5000-score*500, 2000));
}

I suppose each click (score) will decrease the time with 500 millis...

Limit - Math.max(a, b) will choose the biggest value. Means when 5000-score*500 will be lower than 2000, it will choose 2000 millis instead

Only timer method:

public void timer() {
    float time = Math.max(5000-score*500, 2000)
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}