Lessons: 11Length: 1 hour

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.8 Add a Game Over Scene

In a finished game, if a bomb hit the player's ship, you'd typically see something dramatic like an explosion. Well, we're going to skip the dramatics here and instead show a good old game over screen.

2.8 Add a Game Over Scene

Now that we have our collisions down, it's time for us to get beyond the just removing our ship from the parent. And actually present a secondary game scene, maybe a game over scene to the users so they can see that the game is over. And that they can tap a button or something like that to actually restart the game. So the way that we're gonna do this is in two stages. First thing that we're gonna do is we're gonna add in a new game scene, so let's start doing that. So we're going to add in a new file, let's go ahead and add a Swift file. Let's hit Next. Let's give this a name. We're gonna call this the GameOverScene.swift. Now you could call it anything you want. Just remember what you called it. Well, ultimately what you called it doesn't really matter. But the name of the class that you're gonna define within here matters. So I'm actually just gonna drop some code that I've already written in here to save you the time from actually watching me do this. So what we're gonna do here is we're gonna create a new game over scene class. So that's once again gonna be an SK scene. And we're gonna override the init function here. And we're just gonna do some basic setup things that you've seen before. We're gonna set up the background color to be white. We're gonna create a new label that's gonna have the Cochin font. And we're gonna set its text equal to game over. Set some font size, font colors, set its position. We're gonna add it to the scene. And we're gonna do the same thing with a replay button that's gonna say play again. Set up some of its properties once again and we're gonna add it to the scene. Then we're gonna handle some touches once again like we've done before. But in this case, we wanna know when the user has touched the replay button. So we're gonna get its touch location using the touch.location in that we've seen before. Then we're gonna try to get the first node that we've touched. And if the node's name is replay, which we've set up up here. Then, using a transition here, we want to reveal this new scene, which is ultimately gonna be a new instance our game scene. And then we're gonna go ahead and present that which is going to start the game all over again. And that's just some required bits down here that we really don't need to pay too much attention to. But if you do delete it, then Xcode will say, hey, where'd it go? You have to add it back in and then it'll just add it right back in for you. So this is the basic setup of the scene. Nothing too crazy. And in order to tie into this, we're gonna come back to our Game Scene.Swift. And once we're in here, we wanna come back down into our contact here. And the first thing that we were doing was we were just removing our ship from the parent and that's fine. But let's get a little bit more elegant about this and let's present this new scene that we've just created. So to do that, we need to create a new instance of it. So we're gonna say game over scene = to a new instance of our game over scene. And then we can set its size. And its size is going to be equal to self.size since we want it to be the same size as what we had already been using with the current game scene. And then we could go ahead and present this to the user. We can say self.view.presentscene. And there's a couple overrides here. We're gonna use the one with the transition because transitions are cool. So we want to present a scene and that's gonna be our game over scene. And let's use a transition here. Let's say let's use the, I don't know, maybe the doorway transition and we need a time interval. We're just gonna give it a second. So let's go ahead and save all of that. So once we've done all that, now as soon as we get a collision, we should jump into this code here. We'll remove our ship from the parent. We'll get a new instance of our game over scene. And then we'll present it with the doorway transition. So, we'll go ahead and hit run now. And we have a problem here. We're referencing the name of the class here, not the actual variable that we created. So we'll go ahead and change that and save it. Let's go ahead and rerun. Now once this starts up, we should see our bombs again with our ship floating through space. And as soon as we get in the way of one of these bombs, we will detect a collision. We will get our doorway transition. And there you have it, we get Game Over in our font. And then we get a button down here for Play Again. And if we tap this, then we flip back over to another instance of our game scene and we can once again play again. So there you have it. You've basically started building a full side-scrolling spacebomb avoiding infinite runner type game. Going from absolute zero up to a full game with only a little bit of instruction. And just given the basic tools of being able to use some of the built-in features of SpriteKit, and the Swift programming language.

Back to the top