Increasing Score


The course is part of this learning path

Start course

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


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


Hi, within this lecture, we're going to continue building our Kenny app. So far, we have designed our game. Right now, first things first, I'm going to make sure that when user taps on Kenny, it actually refreshes the score, it increases the score and shows it on our score label, and in order to do that, what can we use? If you say GestureRecognizer,

you're absolutely right because that's where I'm going to go. So, I'm going to create a GestureRecognizer, and I'm going to assign them one by one to our Kennys. Of course, first I need a score variable to keep track of this score. I'm going to initialize this at zero, and then later on, I'm going to do what I have to do in the viewDidLoad because that's where it's going to take place. So, I want them to happen when the first user launches this app, like creating a GestureRecognizer. So, I believe you know how to do that. First, let me make sure that scoreLabel.text displays the score. So, I'm going to go for a score, and with a reverse slash over here, I'm going to use my score integer. And then later on, I'm going to go ahead and create my GestureRecognizer and assign it to be my GestureRecognizer in my image views. 

So, I cannot do that with one GestureRecognizer unfortunately, in fact, it makes sense to use one GestureRecognizer, but unfortunately that's not how it works in Swift, that's not supported, so we have to create each recognizer one by one. So, I'm going to call this recognizer one, and this will ask for a target, and of course, the target will be self, and you know why this is right, because we're going to choose this electorate function in our view controller and I'm going to create this function just for once, hopefully this is supported in Swift, So, I can just call it increase score, and all this function is going to do, just increase the score by one and display it on the text scoreLabel.text. 

Okay. So, we have to make sure we put func in here, and in the action I'm going to go for a selector and increase score. So, that's good. I'm going to create this recognizer for nine times. So, I'm going to copy and paste it for nine times. So,  1-2-3-4-5-6-7-8-9.   So, you can change it in a fast way, 1-2-3-4-5-6-7-8-9, and you can choose the same function. Okay, so increase card will do the thing for all of these recognizers, and now I have to make sure that these recognizer's are assigned to related image view. So, I'm going to go ahead and say Kenny1.recognizer, GestureRecognizer.

So, how do we do that? Add GestureRecognizer and we're going to add recognizer1 in here. So, I'm going to copy and paste this nine times as well, and I'm only going to change the numbers. So, this is 4-5-6-7-8-9. And I'm going to assign the related recognizers as well. So, 1-2-3-4-5-6-7-8-9. Okay, that's cool. And in fact, we have to make sure that these images can be clicked, these images can be tapped on. So, before we do that, we have to say Kenny1 is user interaction in Label2. So, I'm going to do that for nine times as well. So after we do this, boring stuff will be over, I promise So, you have to do this though. So, Kenny1 is user interaction enabled. So, I'm going to copy and paste this nine times as well. 

So, 1-2-3-4-5-6-7-8-9. So, let me see, 2-4-3-5-6-7-8 and we need one more, so nine, and this all will be true. So, we are good to go. We have created our Kennys, we made sure that we created recognizers, and we assigned them to be gesture recognizers of the related image views. So far, so good. Now we are making progress. So, let me increase the scoring here. All I want to do is just say score += 1. So, it will add one to the current score. Okay. And, of course, I have to say scoreLabel.text is going to be "Score:" \ (score)" because that will refresh the current label. Okay.

So, I believe we can test this right now. I did that in the viewDidLoad as well. So, this may cause a little bit confusion for you, but if I don't do this here, it will just refresh the score variable but it won't refresh the score label because this code will only get called once, once the viewDidLoad gets called, and after that it won't get called. So, we have to make sure we add this line here. So, as you can see when I tap on this, it increases the score. So, let me try everything in here. So, it seems that it works on all of the Kennys. So, we are good here. So, we managed to increase the score. So, we can move forward. We have to make ways to animate these Kennys, and we have to make ways to countdown from some certain number, and we have to make a way to store our high score, and we're going to do all of this in the following lectures.


About the Author
Learning Paths

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