Part 4 – Solo Mission (Space Invaders) – Make A Full iPhone Game In Xcode

Just another WordPress site

Part 4 – Solo Mission (Space Invaders) – Make A Full iPhone Game In Xcode

hello this is Matt from Matt he knee ups and welcome to part four in our six part full game tutorial series teaching you how to make the full iPhone game solo mission in Xcode using sprite kit and Swift so far we have a ship in space they can fire bullets at enemies we can kill the enemies and the enemies can kill us in this video we will take what we have so far and start to turn it more into a game so today we will set up our score system our level system and our lives system so for our score system the player will earn a point every time they kill an enemy for the live system the player will lose a life every time an enemy moves part of the player and for the level system when the player reaches a certain score the game will level up and enemies will spawn more frequently making the game more difficult so by the end of this video your game will look like this so let’s jump back into Xcode to continue okay so back into Xcode and back into our game scene Swift now as I said today we are dealing with our score system our level system and our lives system for our score and live system we will have a label on the screen which shows to play a score and how many lives they have left so before we can do anything we’re going to do is setup a custom font in this Xcode project so we can use a custom font in this game which we will use in our labels because custom fonts are really easy to setup and they make a massive difference so first things first we will find the custom font file the dot TTFN which is in the assets folder which you downloaded to Getty Images this font is a free to use font which I downloaded from dafont.com and for full credits about this font please check out the video description and we’ll just drag this into our project we want to make sure copy items if needed is ticked and we want to tick just here as well and hit finish so the custom font the bold font is now in our project but we can’t use it just yet we still have to set this up to set this font up what we would do is move into our home area of Xcode this just here we’re moving to build phases and into copy of bundle resources now here all we have to do is make sure the bold font TTFN is on this list if it’s not find the plus symbol and add it if it is already on your list then you haven’t got to do anything here but it is not on this list this will not work so make sure is here on this list then what we’ll do is jump into our info.plist and here we can set up the custom font so we can use it in our project so what we will do is right-click go add a row and the extra row we want to add to our P list is font provided by application if you use a capital F it should find it for you when I open this up and here all we have to do is say the file name for the font so this here the bold font TTFN no spaces in camel case okay so lowercase T capital B capital F exactly as this is here so the bold font TTFN and now just with that we can now use this custom font in the project so back into the game thing okay so we can now do our score system so for our score system all we would do is declare a variable to keep track of our school and then we will simply show this score in a label so we will globally declare in the game scene a brand new variable and we’ll call this a game score I will start this off at 0 so game score will keep track of the score in the game this is a variable so far rather than let because the value of the game score will change if it would let we would not have been able to change the value of game score we also want a label and we are declaring this globally within the scene because we’re going to set it up and it did move to view but then affect it somewhere else therefore we would do this at globally so we will say left call this a score label it’s gonna equal an SK label node so a label object and it will use the font the bold font the name of the custom font so we now have our variable and we have our label so we will now set up our label so into the did move to view and we’ll do it here because we want our label on the scene straightaway and here we will set up our label so score label dot text to take our label and set the text and the starting text is going to say score : 0

it will give this a font size of 70 a font color of white so SK color white color we will give this a horizontal alignment mode of left and what this means is the position that would give this label in a few seconds will be the left of the label and that will always be the left of the label so if our label text got longer so for example if we scored somehow a thousand what happened is the label would expand to the right across the screen rather than outwards from the middle because that way some of the label work might go off the screen so by locking it to the left we are avoiding this potential problem so there is a score label dot horizontal alignment mode equals SK label horizontal alignment mode left so now the left of the label will always stay in place and it will expand to the right and we’re doing this to the left because the position is going to be in the top left hand corner so CG print the X is going to be 15% of the way across the screen because remember we lose some of the width of the scene on some devices so we can’t do all the way to the left so self size width x 9.15 and the y will be 90% of the way up the screen so self dot size the height times nine point nine will give this a z position this will always be on top of everything so we could give it as their position of like five but to make 100% sure is always at the very top the Z position will be 100 the position of the background and the ships and the bullet I know we’re near a hundred so we can safely say this label will always be on top of everything and then we can just add it like so so we now have a score label a starting text will say School : 0 font size a 70 font color of white it’s in the top left hand corner of the screen with alignment mode left so locks the left of the label in place we gave it a very high Z position to make sure it’s always on top of everything else in terms of layers and then we actually made it so we now have a score label we now want to add to our score and display our actual score in this label so we’re going to do is make a brand new function called add score now in here we will do everything that we have to do to add to our score and then simply call this function when we want to add a score so we want to add a score we will take game score of new a variable name we will add one to this and then all we have to do is show this in the label so score label dot text so we’re taking our label and we’re changing the text to say score and now what we want to do is a display game score and to show a variable or a continent in a label we will do a backward slash open bracket game score close bracket okay so with this here our label will show our actual score okay so when we run add score we’re going to add one to our school and then show a game score in the label all we have to do now is tell this when to run now in solo mission the only way that you can get points is by killing an enemy so when a bullet hits an enemy the player will get a point so we’ll move into our did begin contact and we’re going to find where our bullet hits the enemy and now here all we have to do is say add score so now if a bullet hits an enemy add score is going to run so add one to our score and show this in our label and just with that our score system is now set up so we’re going to hit run and we would check this out so now here is our game and as you can see when we hit an enemy with a bullet our score goes up in our label which is in at the top left-hand corner of the screen okay so back into Xcode and we will now move on to our level system in solo mission the level system is going to be when you reach a certain score the game will level up and then the time gap between enemies spawning will get shorter so you have more enemies spawning so you have more enemies to kill so for our level system we will move into the function that we already have called start new level now as you may remember we are calling this straight away when the game begins and at the moment is saying run a sequence to spawn an enemy and then to wait a second and then run this forever so spawn wait for a second spawn wait for a second spawn wait for a second and

so on so we get a nice flow of enemies so here we will set up our level system and then all we have to change is this wait for duration depending on which level we’re currently on the higher the level the shorter the wait for duration so the more often we get enemies now before we throw our level system in place here is one quick thing I want to do and that is to find our sequence and I want to swap these actions around because at the moment as soon as the game begins we get an enemy and then we wait for the second but instead I want to wait for a second and then spawn an enemy just so the player actually has a chance to start the game before their enemy comes down ok so that’s just polishing off a little something when we set the sequence up a few videos ago I mentioned that I wasn’t sure which way round these actions should go in a sequence doesn’t really make a massive difference just a tiny bit of polishing off as I mentioned back then it’s up to you which way around you have this sequence but I now personally think that this way round is better for gameplay support our level system to start with we need a global variable called level number and this will keep track of which level we’re on when I start off at zero and then and then we’re going to do we will call this function when we want to start a new level so whenever we run this code we want to add one to our level okay so the very first time we call it you’ll go from zero to one starting us off at level one we will then run this code every time we want a new level to start and in which case the first thing we do is move from level 1 to 2 2 to 3 3 to 4 and so on depending on how many levels you want okay so what we’ll do is when we start a level we want to completely stop these enemies spawning and then restart this sequence to spawn enemies with a smaller gap so stop with currently doing and then rerun this sort of a different duration so to do this when we run our action we are going to add a key and here we’re going to put spawning enemies so now this action as being run has a reference name as a key and now what we can do is stop that action so we’re gonna do is say if self dot action for key spawning enemies so this down here if that doesn’t equal nil then we can stop it so self dot remove action for key spawning enemies make sure these are the exact same as this down here because otherwise it won’t work what we’re saying is when we run this run it with a key so like a reference name of spawning enemies so now when we start a brand-new level if this is already running and therefore we’re already spawning enemies we want to stop it from spawning enemies so if the scene has an action called spawning enemies because spawning enemies doesn’t equal nothing then remove it so now if we’re spawning enemies stop it because we’re going to rerun this in a slightly different way for the next level so we can now set up our different durations depending on the level so we were saying far and we will call this level duration and this is going to be an NS timer interval since would be a number that is going to be in seconds now in a moment here we’re going to set level duration of value so a duration depending on what level which is going to be and then we can update this down here so the gap between enemies is then what we want it to be for that level so we’re now going to use a switch statement it’s a bit like a big if statement so go say switch for level number you’ll put this in and I’ll explain what it’s doing so case one we want to do is set level duration to be the gap in time between enemy spawning that we want for this level so for level one so level one is going to be I’m gonna put this at one point two seconds so we’re saying take level number and if level number is set to one then set level duration to this if we’re on level two set level duration so there gap between enemies to be one second level free no point eight seconds level four no point five seconds now I only have four levels in my game if you want to add to this then add them here for the default so if level number is not one two three or four then something has gone wrong so I’m going to do we will set level duration to not point five anyway as a bit of a safety backup and also going to print a message to ourselves saying cannot find level info okay so if this isn’t any of our levels then do this so now if we level 1 level duration is set to one point two so one point two seconds level two level duration is set to 1 so one second level

3 level duration is low point eight seconds level 4 level duration is no point five seconds now what we can do and we spawn our enemies is instead of saying spawn every second spawn every level a duration of value so if we’re on level one here I’m gonna have one point two so the wait for duration is one point two seconds if we run level two here we’re gonna have one second level free no point eight seconds so we get a new enemy every new point eight seconds and then level four null point five so we get a new enemy every half a second so that’s all the info around new levels all we have to do now is tell this when to run so we want to trigger this code to start a brand new level if we have reached a certain score so we’re going to move to our function to add a score and here we’re going to say if we reach a certain score then run the code to start a new level I only have four levels so I’m only going to have three trigger points so when you move to level two when you move to level three and when you move to level four so after we’ve added a point we’re going to say if game score equals equals and my trigger points will be ten so that equals ten through the following or game score equals equals 25 or game score equals equals 50 then run start new level two first score reaches ten because we level one to level two if our score reaches twenty five go from level two to level three and if our score reaches 50 go from level three to level four now if you have more levels all you have to do is keep adding two values for here okay and just with that our level system is now set up once we’ve reached a certain score we will run starts new level where we are adding to which level we’re currently on we are stopping enemies from spawning if they’re already running because remember the first time this runs that they will not be running because the game hasn’t started yet we have set how long of a gap is going to be between enemies spawning and then we ran our sequence to spawn enemies with the time gap that we set depending on which level we are on so we now have a level system set up and ready to use – now when you’re playing the game as you get a higher score more enemies will be spawning and therefore you have more enemies to kill therefore the game becomes harder we’re now going to do is set up our lives system okay so in solo mission you start with free lives and you lose a life every time you let an enemy ship pass you and leave the bottom of the screen so that is why more enemies spawning makes the game harder because you have to kill all these enemies if you let free passed it’s game over so for our live system just like our score system we need a global variable called a lives number which is going to equal 3 the starting number of lives we also want a lives able to show our lives on the screen so SK label node font named again using the custom font the bold font okay so we now have a variable to keep track of our lives and we have a label to show how many lives the gamer has left so every time we lose the life we’re gonna take one away from this and then show that number in our label let’s begin by making our label into the dead move to view we will give it some starting text that will say lives free we’ll give it a font size of 70 font color of white so SK color got white color and just like with our score label which is on the left hand side of the screen we set the alignment to left as this labels on the right we’re gonna set this alignment to the right this label will not be getting bigger so the label won’t be expanding but it just makes it easier to position it in place so leaves label dot horizontal alignment mode equals sk label horizontal alignment mode right and we will give this a position so CG point and as our score label is 15 percent of the way across the screen our lives label will be 15 percent of the way in the screen from the right so 85 percent of the way across the self-taught size dot width times 9.8 5 and the wire self dot size dot height times not point 9 again to match our score label said position of a hundred to guarantee this is always at the top because we’re never going to have a dead position higher than 100 and then we can add it self to add child lives label so we now have a label in place to show our lives what we now do is set up a function to take a life away and then simply call it and run that code when we want to take a life away so there’s a funk lose a life and now in here we are going to take one away from life’s number and update our label text to show our new number of lives to take one away and update our label text to

say lives and then show how many lives that we left what we also want to do is make this label kind of bounce so you can tell that you have lost a life if a ship goes past you so go make it get bigger and get smaller just really quick just so it kind of catches your eye so you can see that you have lost a life so you want an action called a scale-up and SK action scale to 1.5 so get 50% bigger over not point two seconds and then scale back down to one over not point two seconds set these up as a sequence SK action dot sequence in the square brackets because it’s a list or an array want to scale up and then scale down and then run this on our label this one action scale with sequence so now when we run this code to lose a life we’re going to take one off of our lives number we’re gonna update our label text and then to make it really really obvious that we have lost a life we are going to run a sequence of actions on our label so our label will get a little bit bigger and then go back down to normal size just so like I said it’s really obvious that you have lost the life so that for now is order the code we need to lose a life all we have to do now is run this when we want to lose a life so in solo mission we are going to lose a life if an enemy ship leaves the bottom of the screen before we can hit it with a bullet so if an enemy ship moves past the player off the screen and therefore we have let it past we are going to lose a life now the vast majority of this code is already in place so this that’s really quick and really easy to do what a do is move into our spawn enemy and here we have the code to spawn an enemy and to move it off the bottom of the screen so this sequence here to move the enemy and then delete the enemy is going to keep running until either it’s finished or we have hit that enemy with a bullet and removed the enemy from the game so all we have to do here is add an extra action to our sequence to say if the enemy does complete moving to the bottom of the screen not only delete it but also lose a life because if it finishes moving that means it would have left the bottom of the screen and would have passed by at a gamer without being hit by a bullet so all we have to do is add an extra action called it something like lose a life action for your SK action run block and then the name of the function to lose a life so this from up here so now that action will run this code here and then all we have to do is say for the enemy if it leaves the screen and it has been deleted run lose a life action now the enemy is going to move down the screen and if it reaches its destination and a basa with a screen without being hit by a bullet because remember if it’s hit by a bullet it is in the lead hit from the game and all of this will stop for that enemy so if it reaches its destination delete to the enemy and then lose a life so that for now is our life system set up in the next video we’re going to deal with game over so if we run out our lives but for now if an enemy leaves the bottom of the screen we will lose it a life so we now have a score system in place we have a level system in place and we have a live system in place so hit run and check it out so now here is our game if we hit an enemy with a bullet is going to add a point and if an enemy leaves the screen will lose a life and our lives label bounces every time we lose a life to make it really obvious that we have lost a life now in a moment our lives is going underneath zero and that’s because we haven’t got a game over yet but we’ll be dealing with game over in the next video so solo mission now has a score system a live system and a level system in the next video we will deal with game over and restarting the game once it is finished but for today that is all as always thank you for watching if you liked watching this video which I really hope you did hit that like button hit subscribe leave any feedback down in the comments and I will see you next time goodbye you