1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Creating a Catch Kenny Game for iOS

Counting Down

Contents

keyboard_tab

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
53m
Students
7
Description

In this course, we're going to create a game for iOS in which you chase a character, and each time you tap him, your score increases. This will challenge you to think about how you would build your own game using your existing knowledge of iOS and then, of course, we're going to code along together to create the game.

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture, we're going to implement counting down functionality into our game and you can imagine that we're going to use timers for that. So, you now know how to use timers.

We can just create a timer and we can just create a time counter, like counter variable and we can make sure that we display this in our counter time label. So, here we have our time label. It can just start with 10 or 30. You can choose it, whatever you want to choose. And then, we're going to create another timer actually, within the next lectures to animate the Kennies as well. So, whatever you decide for the number, you may want to refresh it at this Main.storyboard as well but it doesn't matter. I'm going to do that on the viewDidLoad so it will be on there, even before user sees it. So, let's go. Let's do that. I believe you can do that on your own, by the way. You may want to pause the video and try this on your own if you haven't completed this assignment yet. 

So, this will be our variables and this will be our view. So I'm taking some notes. This is a good practice. So, this will be our images and we're going to have a lot of code in here. That's why I'm doing that. So, after you open your notes, open your code. You will know what you have been up to. So, let me create timer over here so we can reach that timer from everywhere. So, this is going to be our timer. Of course, we're going to redefine this, actually change the value to schedule timer in the viewDidLoad and we will need the counter variable as well, so that we can subtract 1 from this counter every second. So, we did that before. You know how this goes. So, let me go to viewDidLoad. 

In here, first of all, I have to make sure that this timer or this counter will be displayed in the timeLabel. And then, we can actually create our timer with schedule timer. So, this is where we will be doing the timer stuff. So, time labor or counter, I'm going to go for 10 in order not to wait too much between tests. So, you can adjust this number later on. I'm going to make sure that timeLabel.text is the counter. Of course, I cannot say counter because this is an integer. I have to convert this into a string. It will give me an error. And in order to do that, I'm going to do it like this and this will convert this into a string as well or, you can just say string wrapped with counter like this. So, you can choose your own way. So, this will be our counter. This will be our time label, actually. So next, I'm going to take my timer and I'm going to create a scheduledTimer out of this variable. So, this will be scheduledTimer and we want this time interval, target, selector, userInfo and repeat. 

So time interval, I'm going to go for one second one more time because I just want to subtract one from the counter every second. Target will be self and self refers to leave controller. And selector, we're going to create another object to see function and we're actually going to call this countDown or just subtract 1. You can call this whatever you want. I'm just going to go for countDown. So, next step will be to define selector in here. I'm going to go for countDown. So, this is good. Later on for userInfo, I'm going to go for nil and repeats, true. So, here you go. We have our timer. So, what do we want to do in the selector function, in the countdown function? This is going to be fairly easy. We're just going to deduct 1 from our counter variable and then we're going to display that on our timeLabel. So, let's see. counter -=1 and our timeLabel.text will be again, counter of String (counter). 

And then, we can check to see if counter has reached 0. So, we're going to have an if statement in here. I'm going to say if counter = = 0 and { } here and you can go for timer.invalidate(). This will stop our timer and we will do some other stuff like displaying alert message to the user. And in this alert message, we will just provide some replay functions and other stuff as well. But for right now, I believe we can create our alert message only. So, how do you create alert messages? Of course, you use UIAlertController class. So, first let me do the alert and say, let alert = UIAlertController, not ActionController but just controller. If you open parentheses, so you will have a title. So, what do we do for a title? We can say something like "Time's Up" or "Time is Over". And this is good for a title for a message. Maybe we can say, Want to play again? So, that user will know what to do and let me say, do you want to play again? And for the preferredStyle, I'm going to hit Enter and see the . and then say alert. So, do we have a button in here? We have a Replay Button and we have No Button. 

So, we have to create two actions and you know how to do this. You just have to use UIlertAction in order to do that. Let's start with the cancel button. So, I'm going to call this okButton and I'm going to say UIAlertAction. And the title can be Cancel or OK. And the style, let's go for Cancel this time to see how this looks. So, the default will be more highlighted. And the handler, we don't need any handler. We don't want anything to happen when the user taps on this cancel. So, we're going to have a replay button as well. So, that's where we want handler. In this case, I don't want any handler. So, let me create the replay bottom now. So, replay button will be again a UIAlertAction. And then, we're going to go for replay and then for style, I can go for default this time. And handler? We want handler because we have to have a Replay function in here. So, hit Enter and hit Enter one more time to make this visible. And in the coding section, we're going to write our replay function. Right now, I'm not going to do that. We're going to do that last. So, we have to add these buttons to our alerts. 

So, alert.addAction, okButton, alert.addAction, replayButton. And then, we are ready to present this view. So, I'm going to say self.present and it will ask me what to present. So, I'm going to present this alert and it will be animated. Why not? So, let's say true and completion. I don't need any completion because I'm doing what I had to do in the replay button. So far so good. Let me test this actually because we don't have to write anything else at this point. We're just going to take care of thos things later on at the end. So far, I don't know what to do to create a Replay function. So, here we go. It's already started counting down from 10 and as you can see, I can still click on this course. And here you go, Time's Up. Do you want to play again? So, if I click on Replay or OK, nothing will happen. So, because we didn't write our handler, obviously, we didn't write our replay function. We're going to do that later on. But right now, I can say that we are making progress and we are ready to animate the Kenny. So, we're going to stop here and that's what we're going to do within the next lecture.

 

About the Author
Students
1176
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics