vendredi 2 décembre 2022

Can't call a function from CountDownTimer object. What am I missing?

I'm going through Googles Kotlin Compose tutorials. One of the tasks is to build a game where you unscramble words. After completing it, I tried to improve the game on my own, and just can't figure out how to add a countdown timer to it. I want the program to skip a word when time runs out.

I'm a programming noob, it's not quite clear to me yet how classes and objects work and how they differ from functions.

The code for the timer at the moment:

object Timer: CountDownTimer(60000, 1000) {

    override fun onTick(millisUntilFinished: Long) {
        TODO("Not yet implemented")
    }

    override fun onFinish() {
        skipWord() // <<----------- **Unresolved reference: skipWord**
    }
}

Elsewhere in my code I have:

class GameViewModel : ViewModel() {

//....

fun skipWord() {    // <<---------------- Function that skips to the next word
    updateGameState(_uiState.value.score)
    updateUserGuess("")
}

//.....

private fun pickRandomWordAndShuffle(): String {
    // Continue picking up a new random word until you get one that hasn't been used before
    if (currentLanguage == "English") {
        currentWord = engWords.random()
    } else {
        currentWord = finWords.random()
    }

    setPointAmount()
    Timer.start() // <<---------------Start a new countdown for a new word.

    if (usedWords.contains(currentWord)) {
        return pickRandomWordAndShuffle()
    } else {
        usedWords.add(currentWord)
        return shuffleCurrentWord(currentWord)
    }
}

}

Also, a separate problem: the .random() always uses the same seed and picks the same words to unscramble.




Aucun commentaire:

Enregistrer un commentaire