3.9 Restarting the Game
One of the most important aspect of creating/playing games is “replayability”. You want the user to keep playing as much as possible. At this point, when the game ends, there is no way to restart it—let's fix that.
1.Introduction3 lessons, 07:37
2.Introducing SpriteKit7 lessons, 54:48
3.Building a Game11 lessons, 1:30:25
4.Conclusion1 lesson, 01:49
3.9 Restarting the Game
Now that we can visually see when we've actually won or lost the game, let's provide a mechanism to get back to the original game scene to actually try again. So this is gonna be pretty simple. We're gonna go back into our game over scene and into our visual designer. We're gonna grab another label. And we're going to put it below the you lost, and once again we're going to come in here and we'll set this equal to chalkboard and we'll make it a little bit bigger. Maybe not 144, maybe 64, 72? Maybe 64. That's fine. And then we'll say in here just tap to play again. Something like that. So what we're going to do now is a very similar thing, is we're going to give this a name. So that we can actually interact with it. And determine when someone has actually touched it. So we'll come in here and we'll say that this is going to be again. And we'll save that, just like that. And we'll come back into our game overscene.swift and now we are gonna want to deal with some touches again, so we are going to come into, say touches begin and we're simply going to say when we've touched the screen. Or actually at any point, we don't really necessarily care that you have to, have to touch this, this text. We're gonna leave this pretty generic. So let's not worry about giving this a name. So we'll save that. And we'll come back over here and we'll just say, if you tap anywhere on the screen. So we just need to detect a very simple tap. So we'll say, if let view equal to view. And within here, we're simply going to let our scene be equal to, we want to get our game scene, dot, we want to unarchive from file, and that's once again game scene. So, this is gonna be very similar to what we did before. Cast this as game scene. And then we'll say view.present scene, scene, we'll save that and now when we come back and run our game again. Hopefully this time we will actually try to, win the game. So we will have this kind of bounce around here a little bit. And once we have won, we should see the You Win! And we do, and if I tap in the middle again, then we start over again. So we can kind of go around into an endless loop here, just kind of play this level, and once it's, once I told you before, that you can actually begin to create levels. Like we've started to design within our gamescene.swift file, we've started to create this concept of being able to place blocks around. So, at this point, a nice extension point would be to take these tips and tricks that you're learning about. Dynamic, dynamically positioning these nodes out on the scene and transitioning from one scene to the next. That's probably a pretty good mechanism to continue to use to add on additional levels however you would like. Now we're kind of coming to the end here. We're gonna do a couple of wrap up things. Here in the last couple of lessons to kind of give this a little bit more finish. We'll add a start scene in there. And kind of introduce maybe another interesting topic that might become useful you, for you within your games and that's gonna be using this little hidden gem down here of our update function. So we'll talk about that a little bit as well, but then from that point, I will leave all the extending of this application to you to take it to whatever sort of levels you're interested in doing.