In most games, you'll want to change between multiple scenes. This tutorial shows how to create a custom scene and connect it to a custom class.
- [Instructor] In just about every game, there's more than one screen, or scene. Let's say you're making a game, and you prototyped some game play in your GameScene.swift file and in your GameScene.sks file. What you're ready to do now is add a main menu as you're developing out your game. So, let's look at making a new SpriteKit scene. To do that, I'm going to go to File, New, File.
Here, under iOS, I'm going to scroll down, and I'm going to find the SpriteKit section. This is under Resource. In here, you're going to see SpriteKit Scene. That's what we want. Select that and hit Next. I'm going to give this scene a name now. I'll call it MainMenu. Then I'll hit Create. So, in here, we have this menu. We have the interface just like we have in our GameScene file.
It just doesn't have the same background, and it may not be the same size. You can, of course, modify that, by selecting the scene, and heading over to the Attributes Inspector. In here, we can change the background color to lead or black, for example. We can change the size to an iPhone SE, change it to Portrait instead of Landscape. and it pretty much looks the same. We can also lay out our user interface. In Object Library, I can filter for a Label, for example.
Click and drag one out to the screen, and then I can, with the label selected, head over to the Attributes Inspector and change its Text property. I'll type out Main Menu. Press Return on the keyboard. Now, I'm not so concerned with how big the font is or where it's at. Just know that you can modify those settings in the Attributes Inspector, should you choose. You can click this T button in the Attributes Inspector, and then choose a different font and font size, if you will. Now, let's say our goal is to have this menu appear when you launch the application.
What I'm going to do is save the file, and then I'm going to head over to GameViewController.swift. Remember, this is the file that actually loads the initial view. That's inside of ViewDidLoad. In here, we create the scene. All we need to do is change the file that we load initially. So, instead of GameScene, we want to load our other .sks file, which is MainMenu. So, the file is going to be MainMenu.
And that's all there is to it. We run the application, and instead of seeing the other scene that loads the ship, we should see the main menu. And there it is. We've successfully loaded the main menu. So, I'm going to stop the app and return to Xcode, and I want to show you one more thing, really quick, and that is how we can connect a custom class to a SpriteKit object, whether it's a sprite, or an .sks file, or what have you.
I'm going to select MainMenu.sks and press Cmd+N to create a new file. And then I'm going to create a new class. This will be a Cocoa Touch Class under the iOS template category. I'll hit Next. I'll call this MainMenu. And then you want to make this a subclass of the class that you're working with. So, right now, we're working with an SKScene, but the same principle would apply if you wanted to subclass a sprite. So, you would put SKSpriteNode if you were subclassing a sprite here.
I'll click Next from here. Save it in the default location. And there's my MainMenu.swift file. Now, I'm getting this error here because it's not importing SpriteKit. It's importing UIKit. I'll just change that to SpriteKit, and that should fix everything. We've got our MainMenu that's an SKScene, and we can connect it to our class by selecting it, and then heading over to the Custom Class Inspector, and then setting the Custom Class.
I'll just call this MainMenu, which is the name of my class, and then hit Return. I'm not going to do much in here, other than just create a Print statement. We have that didMove(to view) method. So, I'll handle that, and then I'm just going to print out "custom class running!" Now we can go to GameViewController.swift, and we can also, instead of creating a new SKScene, we could create a new MainMenu. Let's run the app and just confirm that that print statement is working.
I ran this, and I should be able to stop it and then look at the debug area and find that "custom class running!" code. So, we can create our class file by creating a new .sks file. It we want to connect that, or any object in SpriteKit to a custom class, you select the object, and then you head over to the Custom Class Inspector. and you put the value right in there.
- Creating a SpriteKit project
- Adding and modifying sprites
- Creating sprites in code
- Transitioning between scenes
- Working with children and parents
- Adding physics
- Detecting and responding to collisions
- Applying particle effects
- Creating actions
- Working with sound
- Adding cameras and lights