Replay Function


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 start writing our Replay function. So, that a user taps on the replay, the score will be reset and the time will be reset, and Kenny will start animating once more. So, let's go to our Replay function. So remember, we have to write this in our replay button, where we control if counter is zero. So, we invalidated our timers, so we need to start timers one more time, and then, we also have to make sure that our score label or score variables are reset. So, I'm going to say score zero, and it gives me an error, because we are inside of what is called a closure. So, we have to make sure that this is self.score. Why we're doing this? Because the Xcode thinks that maybe we created an another variable called Score. 

Right now, in order to reach this score, we have to say self.score. Maybe, I created another variable over here called Score, and in order to make sure that I'm not referring the score, rather, I'm referring the variable that I have created previously, I'm saying self.score. And don't ever do that, if you have some variable called score, don't ever come here inside of a closure, and say that, okay, I'm going to create another variable that has exactly the same name as before. So, just create another name. So, here you can just say self.score as well, but you cannot create another score in here, so that won't be any problem. But, in here, it's inside of a block and inside of another block, so you have to say self in order to make sure that you are referring to the correct variable. So, I'm going to say self.scoreLabel.text, score\self.score And, after that, of course, we have to do that for the counter as well. 

So, I'm going to reset our counter and timer label, okay, time label. So, this will be self.timeLabel.text. So, string representation of self.counter, and after that we have to make sure to call these timers one more time. In order to do that, let's go over here and just copy and paste all of these values to replay function below. So, let's go here and paste. And, I believe we have to say self.timer in order to make sure this works. So, self.timer, okay, let's fix it. And you have to say self.countDown, self.hideKenny, and self.timer, self.hidetimer, in order to make it explicit. So here we go, we are ready. We have finished our replay function; let's see if this works. So, let's run our game, and let's try to hit on Kenny. As you can see, I can tap on Kenny and score increases, so time is running out. And, once I reach the zero, I can say replay. If I say replay, time will start one more time, and score is zero now. If I tap on Kenny, it will increase. 

So, this works in a perfect way. This is what we wanted. If I say, okay, this will be zero, and score will be seven, but I cannot tap on Kenny anymore. So we're going to stop here, actually, and within the next lecture, we're going to take care of high score. Because, in here, we only reset all the variables. We reset score, reset counter, reset labels, and we recall timers, but we don't take care of high score. So, we're going to have to do that separately. Let's do that in the next lecture.


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