Is there a way to add 3 seconds to the CountDownTimer when the user gets 3 answers correct (nonconsecutive) and when the timer is running? For an example if the user gets 3 answers correct when the timer displays 10 and it should increase to 13 seconds. This is the code I've implemented and it doesn't work.
class GamePage : AppCompatActivity() {
var correctCount = 0
var wrongCount = 0
val timer: TextView = findViewById(R.id.timer)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game_page)
val finish = Intent(this, GameOver::class.java)
var timeCount : Long = 50000
fun counter(timeCount: Long){
var countdownTimer = object : CountDownTimer(50000, 1000) {
override fun onTick(millisUntilFinished: Long) {
timer.text = "Seconds remaining: " millisUntilFinished / 1000
}
@Override
override fun onFinish() {
if (correctCount !=0 && correctCount % 5 == 0) {
finish.putExtra("correct", correctCount.toString())
finish.putExtra("wrong", wrongCount.toString())
startActivity(finish)
} else {
counter(5000);
}
}
}.start();
}
counter(timeCount)
}
CodePudding user response:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
CodePudding user response:
Why not coroutines? You can easily modify the counter, and the timer will increase.
fun counter (timeCount: Long) {
var counter = timeCount
lifecycleScope.launch(Dispatchers.Main) {
while (counter > 0) {
delay(1000L)
counter -= 1000
if (isAnswerCorrect()) {
counter = 3 * 1000L
}
}
// timer finished
}
}
CodePudding user response:
You could create a countdown Flow like this:
fun countdownFlow(duration: Duration) = flow {
val endTime = System.currentTimeMillis() duration.inWholeMilliseconds
while(isActive) {
val timeRemaining = (endTime - System.currentTimeMillis()).coerceAtLeast(0L).milliseconds
emit(timeRemaining)
if (timeRemaining == Duration.ZERO) break
delay(1)
}
}
And then use it like this, where you cancel your coroutine and restart it if you want to increase the timer.
class GamePage : AppCompatActivity() {
var correctCount = 0
var wrongCount = 0
val timer: TextView = findViewById(R.id.timer)
private var remainingTime = Duration.ZERO
private var countdownJob: Job? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game_page)
startCountdown(50.seconds)
}
private fun startCountdown(duration: Duration)
countdownJob?.cancel()
remainingTime = duration
countdownJob = lifecycleScope.launch {
countdownFlow(duration).collect {
remainingTime = it
timer.text = "Seconds remaining: ${it.inWholeSeconds}"
}
if (correctCount !=0 && correctCount % 5 == 0) {
val finish = Intent(GamePage.this, GameOver::class.java).apply {
putExtra("correct", correctCount.toString())
putExtra("wrong", wrongCount.toString())
}
startActivity(finish)
} else {
startCountdown(5.seconds)
}
}
}
private fun addTime(duration: Duration) {
startCountdown(remainingTime.milliseconds duration)
}
}