Create the Player Class

While it’s possible to add all of your code to the GameScene class, it’s usually not the best plan and can create problems further down the road when you want to extend your game’s features. Instead, you’ll create a separate Player class to keep your codebase free of the muck that causes spaghetti code.

To further organize your code, you’ll put your new class in its own file. From Xcode’s App menu, select File New File... or press N on your keyboard to create a new file:

images/AddingAnimationAndMovementWithActions/new-file.png

When prompted, select the iOS Swift File template as shown in the image, then click Next.

images/AddingAnimationAndMovementWithActions/new-file-template.png

In the Save As field, enter the name, Player.swift and then click Create. This action opens the Source Editor on the right, with the newly created file loaded.

Now, replace the contents of Player.swift with the following:

 import​ ​Foundation
 import​ ​SpriteKit
 
 class​ ​Player​: ​SKSpriteNode​ {
 // MARK: - PROPERTIES
 
 // MARK: - INIT
 }

This code sets up the foundation for the Player class by importing the SpriteKit framework and providing the class definition. By adding the MARK: prefix to the comment (//), you’re able to add a heading in your jump bar and minimap. When you also add a hyphen (-), you’re able to add a separator line. Adding the hyphen before the heading content places the separator bar above the heading; adding it after the heading content places the separator bar below the heading. The following image shows the headings and separator lines as they appear in the jump bar:

images/AddingAnimationAndMovementWithActions/jumpbar.png

The next step is to get the textures loaded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset