The course is part of this learning path
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.
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 find a way to animate our Kenny. And as I said before, the way I'm going to do this is to hide all of these Kennys, and then select one to display it only. And we're going to just change the selected one in a second or in a half a second, so that Kenny will be animating through the screen. And in order to do that, of course we're going to use timers again because that's an operation that we want to do in a given period of time, and we want it to be working on the background without blocking of all the user interface. But before we go ahead and do that, I believe first we have to create a function to test and see how to hide Kennys. So, we're going to take steps one by one. First, hide all the Kennys. Second, find a way to randomly choose one of the Kennys and display it. And then later on chart, make this into a timer. So, I'm going to create a kennyArray, in which we will have a UIImageView.
Okay. So, within this array, I will just add all of these Kennys. So, why am I doing that? Why am I adding all the Kennys inside of this kennyArray? Because actually that's a lot easier to do when you have an array. We can have for loop, right, with this array. You will see what I mean. Let me just create this array. I will say kenny1, kenny2, kenny3 and I will add all of the image views in this array. So, we're already working with a lot of variables, a lot of images. So, I'm trying to make this as simple as possible, as efficient as possible. Okay. So, I'm going to show you a good way to do this. So, if I want to, I can actually make all of this kennyArrays with that .append as well, but it's not very efficient. So, you can just write it this way as well. Right? So, right now if I call one of these elements, what will I get? I will get the imageView itself, right? So, if you write kenny, you will see that this kennyArray is actually a UIimageView. So, if I get one of these elements, I can reach their properties and attributes, and do whatever I want with them. So, let me create another function over here, okay?
I'm going to call this function hideKenny, and let me write it this way. So, if I call one of the Kennys, I can make it invisible. So, how do I do that? There is a method to do that, and you can just say isHidden. So, it's fairly easy. So, If you say kenny1.isHidden, okay, this way, and if you set it to true, then kenny1 will be hidden, kenny1 will be invisible. So, I can do that for all of the Kennys one by one, but I don't want to do that one by one. I want to do that with a loop, that will be much more easier for me. That's why I created this kennyArray. I'm going to say for kenny in kennyArray. Now I can reach all of these Kennys one by one in a loop, and I can assign all these elements to a variable called kenny, and I can just say kenny. As you can see this is still a UIImageView, kenny.isHidden = true.
So, rather than doing this in nine lines, now I'm doing this inside of a loop. So, it's kind of cool. Let us test this. So, let me call the hideKenny function in our viewDidLoad, because if we don't do that, it won't even get called and it won't be executed. So, let me run the simulator, and let's see if we manage to hide the Kennys. So, here we go. We don't see any Kennys right now, but we see the countdown timer and we see the score. We cannot tap on anything because they are hidden. So, that's good. That's step number one. We have hidden all the Kennys. Now, step number two, I want to have a way to select a random Kenny and make it visible. So, how do we do that? First of all, If you don't know how to create a random number, that's perfectly okay. It's arc4random(). Okay. If you just call arc4random method, this is a built-in method that you can use in Swift. And this will ask you for an upper bound if you choose this arc4random_uniform, and if you say 9, for example, to this upper bound, it will return a value between 0 and 9 randomly.
So, you will get 0, you will get 2, you will get 3, depending on totally random algorithms. So, for our upper bound, of course, I want something between 0 and 8. Why 8? You will see in a minute. But right now, it asks me for a UInt32 bit. So, this is an integer, so this is a regular integer, but it's 32-bit. Okay. If you just write a number in here, it will be accepted like 10. But rather than saying 10 or 8 or 0 or 5, I'm going to go for kennyArray.count because it will make much more sense. So, if I say kennyArray.count - 1, this would be the exact number that I need, because kennyArray.count will give me 9, and I don't have an index 9 in the kennyArray. I only have 0, 1, 2, 3, 4, 5, 6, and 7, 8. So, I can call it like this. I can say kennyArray, and it will give me kenny1, and I can say kennyArray, and it will give me kenny9. So, I have to have something between 0 and 8.
There is no kennyArray9. Okay. So, I'm not going to go for kennyArray.count, but rather -1. So, this will give me a number between0 and 8. So, I'm going to use this random number to call the Kenny in a random fashion. And let me create a variable in order to assign this as a value. Okay. I'm going to call this random and you have to convert this into an integer because that's not going to give you an integer, it's going to give you a UInt32 bit. Okay. And I can call this random number inside of kennyArray, and I can just say that isHidden=true, right, or isHidden=false, right? This will make it visible one more time. So, step two is actually completed. So, I'm choosing a random Kenny, and I'm making it visible. So, I am hiding everything in here, and then I'm making one visible. So, let's try this. So, we're going to see one Kenny, that's working and we can click on it. Let's run this one more time.
So, most probably, we'll see another Kenny visible like this. So, it works. Now we have to come up with a way to make this constantly rather than doing this manually over here. So far, so good. We have completed step two. Now, we are heading towards step three. In step three, we're going to use timers one more time. And I'm going to create another timer for that. I'm going to call this hideTimer. So, can we do that within same timer here? Actually, we can do that, but I'm not going to, and I'm going to explain you why. So, let me choose scheduledTimer here. So, in the scheduledTimer, I'm going to give timeInterval as 0.5, target as self, and for selector, I'm going to go for hideKenny, right? Because I have already my function in here, that's what I want to run with this hideTimer. And as you can see, we have to add objc, so userInfo nil, and repeats true. So, this is going to get run once I run this.
So, why am I writing this into a completely separate timer? And don't forget to invalidate this as well or else this Kenny will be circulating, Kenny will be animating all the time. So, we're going to call this hideKenny. And the reason that I'm doing this in a different timer is basically, I need another time interval. So, as you can see, in 0.5 seconds, Kenny animates. So, this is cool. So, this is what we wanted from the beginning. And as you can see, once the timer is done, Kenny stops. And of course, I can hit 'Okay' and Kenny is still there. So, I can tap onto Kenny and it's kind of a bug in our game. So, we need to fix this as well. And I believe the easiest way to fix this would be to copy all of this hiding thing, hiding for loop and paste it in our if control. So, if timer is ended, if timer has reached to zero, then I'm going to invalidate all of the timers, and I'm going to hide the Kenny over here. Now we need to go and actually write our replay function and then Highscore function as well.
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.