0
votes

I have a countdown timer in my code. This countdown timer is supposed to tick every 10ms, and up a textview's number by 1 until it reaches the users score they got during the game.

It's not even reaching half of the value. Here is my code:

new CountDownTimer(lastScore*10, 10) {

        public void onTick(long millisUntilFinished) {
            tempScore++;

            TextView lg = (TextView)findViewById(R.id.lastgame);
            lg.setText("LAST SCORE: " + String.valueOf(tempScore));
            lg.setTypeface(eightBitFont);
        }

        public void onFinish() {

        }

    }.start();

Lastscore is the score they got during the game. tempScore is just used to store the new value to change the textview to.

EDIT: A user pointed out that 10ms might be too fine of a resolution. With 10ms resolution, I had a lastScore value of 115, and tempScore only got up to 46. Changing it to 30ms, I got up to 108 or so, and changing it to 50 has gotten me even closer. It seems I'm going to have to use a value of 100ms ticks to get it to be the right value. But that's a bit slow for what I want to achieve. Is there a better way to do what I'm looking to achieve?

1
Did you checked what is "lastScore" value ?Shadab Ansari
Yes. Without using the timer, and using the 3 lines of textview codes, and settting the value to lastScore shows the correct valueJosh Beckwith
Hi @Josh, What is exactly the value of lastScore (give us an example you are testing with)....the CountDownTimer constructor receives as first parameter the time in millisecods that the count down should run for.velval
I meant - what is the value of lastScore before starting the timer ?Shadab Ansari
10 milliseconds could be a resolution too high for finding a UI element, updating its value and setting its font. Try 100ms and also move your findViewById() and setTypeface() calls outside of onTick().Levon

1 Answers

0
votes

Each time you tickle you countdown, you call an expensive method findViewById. You have to assign it outside of the timer. So your code might look like:

TextView lg = (TextView)findViewById(R.id.lastgame);

new CountDownTimer(lastScore*10, 10*1000) { // set to be called each 10 seconds

        public void onTick(long millisUntilFinished) {
            tempScore++;
            lg.setText("LAST SCORE: " + String.valueOf(tempScore));

        }

        public void onFinish() {

        }

    }.start();