Your First Kotlin Android App · Starting and Ending the Game | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4936497-your-first-kotlin-android-app/lessons/28

What does “this” mean in the endGame function?

@rbondoc Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @shah95rukh, the this keyword refers to the current instance of the MainActivity class the code is currently being run in. The MainActivity instance is being passed to the Toast.makeText function as the context argument. This will tell the Toast class that the text should appear over this MainActivity.

@rbondoc, please, help me! In this string: tapMeButton.setOnClickListener { view → incrementScore() }
Android studio send warning (Parameter ‘view’ is never used). What’s wrong?

@rbondoc Can you please help with this when you get a chance? Thank you - much appreciated! :]

‘context: this’ does not work for the Toast.makeText call. I had to use ‘getApplicationContext()’ instead
I am using Android Studio 4.0

I don’t understand, the final code it’s different from the video. I uploaded because I am having an error with countDown.

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

internal lateinit var tapMeButton: Button
internal lateinit var gameScoreTextView: TextView
internal lateinit var timeLeftTextView: TextView

internal var score = 0
internal var gameStarted = false

internal lateinit var countDownTimer: CountDownTimer
internal val initialCountDown: Long = 60000
internal val countDownInterval: Long = 1000

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    tapMeButton = findViewById(R.id.tapMeButton)
    gameScoreTextView = findViewById(R.id.gameScoreTextView)
    timeLeftTextView = findViewById(R.id.timeLeftTextView)

    tapMeButton.setOnClickListener { view ->
        incrementScore()
    }

    gameScoreTextView.text = getString(R.string.score, score)
}

private fun resetGame(){
    score = 0

    gameScoreTextView.text = getString(R.string.score, score)

    val initialTimeLeft = initialCountDown / 1000
    timeLeftTextView.text = getString(R.string.timeLeft, initialTimeLeft)

    countDownTimer = object : CountDownTimer(initialCountDown, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            val timeLeft = millisUntilFinished / 1000
            timeLeftTextView.text = getString(R.string.timeLeft, timeLeft)
        }

        override fun onFinish() {
            TODO("Not yet implemented")
        }
    }
    gameStarted = false
}


private fun incrementScore() {
    if(!gameStarted){
        startGame()
    }
    score += 1
    val newScore = getString(R.string.score, score)
    gameScoreTextView.text = newScore
}

private fun startGame(){
    countDownTimer.start()
    gameStarted = true
}

private fun endGame() {
    Toast.makeText(this, getString(R.string.gameOverMessage, score), Toast.LENGTH_LONG).show()
    resetGame()
}

}

2020-10-06 16:30:06.969 18172-18172/? E/ple.timefighte: Unknown bits set in runtime_flags: 0x8000
2020-10-06 16:30:15.479 18172-18172/com.example.timefighter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timefighter, PID: 18172
kotlin.UninitializedPropertyAccessException: lateinit property countDownTimer has not been initialized
at com.example.timefighter.MainActivity.startGame(MainActivity.kt:71)
at com.example.timefighter.MainActivity.incrementScore(MainActivity.kt:63)
at com.example.timefighter.MainActivity.access$incrementScore(MainActivity.kt:11)
at com.example.timefighter.MainActivity$onCreate$1.onClick(MainActivity.kt:33)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I was having issues where the Toast was not showing. I fixed this by resetting the emulator and wiping the data.

click the device icon in the upper right of android studio, find your emulator and click the down arrow on the right. Select “wipe data”

HI, just wondering what is the logic behind creating the startGame() function, You put the logic in the ‘if’ statement first and then decided to make the startGame() function. Just wondering why that is? is it just best practice or is there another reason :slight_smile:

This video seems to constantly get skipped over when I try to play it.
The others play just fine. Is it just me, or is something wrong with it?

my timer starts from 6 and then counts down to 0… 6 5 4 3 2 1 0
how can I fix this please