Teki – The timer

In the very last lecture we have added our heroine, Teki, the turtlegirl. When we start the game she will wait for our touch and then she will run, and the background will move also to the left. She will run indefinitely if we leave it as it is now.

To have a harder game we will increase the speed. In this lecture we are going to add a Timer. This timer will be responsible to count later on our scores, and to increase the speed over time.

Every 20 seconds the velocity will be increased by 2. Every time when you could reach this 20 second, your level will be also increased by 1.

So let’s start coding. 🙂

Step 1 – Add the Timer

In our app we are going to use the java.util.Timer. It schedules an action to be executed after the specified delay, expressed in milliseconds.

First of all we have to declare it in our TekiTutorial::class.

var timer: Timer = Timer()

Step 2 – startTimer()

In the create() method we don’t have to do anything with it. We are going to make for it a separated function, called startTimer(). We have to do this, because later on we need to call also, when we want to restart the game, and of course our code will be more transparent.


startTimer

Here we called the schedule() method on our timer variable and we set the delay to 1000. It is measured in milliseconds, so it means 1 second.

Step 3 – Call the startTimer() method

We have to start the timer when we click on the Play and on the Restart buttons, but we are going to implement them later. What we can do now is to add it when the game is not running. Currently when we touch the screen, the game will start. So the call of the timer method goes to the gameState, when it is equal to 0.

Step 4 – stopTime()

Currently we don’t have any Game Over screen in our game, but we will define the stopTimer() method here, and we can add it to our if-else statement of the gameState check.


startTimer

To stop the timer we have to call on the timer variable the cancel() method. It is going to cancel all of the executions of the timer.

Finally add it to the if-else statement in the render() method, where the gameState is equal to 2.


startTimer()

Step 5 – Run the game

After the implementation of the Timer, finally we can run the game and test it. As we have talked about, inside the game we won’t see anything. We can check if it is working corrently, when we open inside our Android Studio the Logcat. You can find it at the bottom of the window.

Open the Logcat and there you can find a filter. Type in the tag of our log messages, so the “timer” word and select “Verbose”. Then you should see this.

TekiTutorial::class

As always, here is the whole source code for the TekiTutorial::class.


TekiTutorial::class

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *