android - countdown with decreasing time

jle

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();
    }
}
kristyna

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
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting a countdown timer with time picker dialog android

From Dev

Decreasing the overall computation time

From Dev

Start countdown timer when time is chosen from Time Picker in android

From Dev

Android : Decreasing size of the Button

From Dev

Decreasing size of CGPath with time in SpriteKit?

From Dev

Android TimePickerDialog selected time not passing through to the actual Countdown Timer

From Dev

Decreasing certain amount of time from a selected time

From Dev

Android cropping and decreasing video size

From Dev

setInterval countDown update time

From Dev

Noda Time Countdown

From Dev

Countdown reset on specific time

From Dev

Time CountDown in angular 2

From Dev

Countdown Clock to a Specific Time

From Dev

Countdown timer, not displaying time

From Dev

How to make countdown with time()

From Dev

MongoDB padding factor decreasing over time

From Dev

matplotlib animation movie: quality of movie decreasing with time

From Dev

Decreasing loading time of HTML5 Video

From Dev

Decreasing processing time for shp map choropleth

From Dev

Android Dialog with CountDown TImer

From Dev

Android ProgressBar timer countdown

From Dev

Android Dialog with CountDown TImer

From Dev

Faster Countdown timer in android

From Dev

Countdown MVVMCross with Xamarin Android

From Dev

Resuming Android countdown timer

From Dev

Making a countdown to a time every day

From Dev

Showing a time countdown progress bar

From Dev

Countdown Clock Synchronized With Time of Day

From Dev

Countdown timer changed with local time