Original: Swift language combat promotion-Chapter 9 game combat-Parkour Panda-2 Create panda class
After we have created the project file, we can start to write our game step by step according to the list we included earlier. Now let's create a Panda class Panda.swift. We will take a decomposed approach and complete the preparation of Panda.swift step by step
First, we have to import the SpriteKit framework
import SpriteKit
Then we create an enumeration value to record the different states of the panda, which are running, jumping, two-step jumping, and rolling.
enum Status: Int {
case run = 1, jump, jump2, roll;
}
note:
In Swift, when you set an integer value to the first name of an enumeration, the following names are automatically filled in order. For example, after run = 1 above, jump is 2, jump2 is 3, and roll is 4.
Then we want to make the Panda class inherit from SKSpriteNode
class Panda: SKSpriteNode
At this time, the complete code of Panda.swift should look like this.
import SpriteKit
enum Status: Int {
case run = 1, jump, jump2, roll;
}
class Panda: SKSpriteNode {
}
After finishing the above steps, let's implement a simple function to display the panda in the scene. We only need to pass 3 parameters to the constructor of Panda's parent class, SKSpriteNode, in the constructor. These are the default color (texture) and the default size (texture) of the texture and texture, respectively. So what we have to do is first write a constant to get the collection of art and science that runs this action
let runAtlas = SKTextureAtlas (named: "run.atlas")
Then use the first art of running this animation as the default art in the constructor.
let texture = runAtlas.textureNamed ("panda_run_01")
Then use the size of this texture as the default size of the Panda class
let size = texture.size ()
Then use SKColor.whiteColor () to get the color value of white
Finally, these are passed as parameters to the parent class of Panda, which is the constructor of SKSpriteNode.
super.init (texture: texture, color: SKColor.whiteColor (), size: size)
The complete code should look like this
import SpriteKit
enum Status: Int {
case run = 1, jump, jump2, roll;
}
class Panda: SKSpriteNode {
let runAtlas = SKTextureAtlas (named: "run.atlas")
// Constructor
override init () {
let texture = runAtlas.textureNamed ("panda_run_01")
let size = texture.size ()
super.init (texture: texture, color: SKColor.whiteColor (), size: size)
}
required init (coder aDecoder: NSCoder) {
fatalError ("init (coder :) has not been implemented")
}
}
When we have finished writing the above code, we will display the panda in the game scene GameScene.swift. Then we must first declare a variable in the scene class, the type is the Panda we just created.
lazy var panda = Panda ()
note:
Using the lazy attribute allows variables to be instantiated the first time they are used, which allows the scene class to reduce overhead when it is created.
Then write the following code in the didMoveToView method to display the panda on the screen.
let skyColor = SKColor (red: 113/255, green: 197/255, blue: 207/255, alpha: 1) // The background color of the scene
self.backgroundColor = skyColor
panda.position = CGPointMake (200, 400) // Panda gives an initial position
self.addChild (panda) // Display the panda in the scene
Press command + r, run it to see the effect
Congratulations, we have bravely taken the first step. At this point the complete code in our scene class should look like this:
import SpriteKit
class GameScene: SKScene, ProtocolMainScene {
lazy var panda = Panda ()
override func didMoveToView (view: SKView) {
// The background color of the scene
let skyColor = SKColor (red: 113/255, green: 197/255, blue: 207/255, alpha: 1) self.backgroundColor = skyColor
// Set an initial position for panda
panda.position = CGPointMake (200, 400)
// Display the panda in the scene
self.addChild (panda)
}
My public account
The broken book I wrote: "Swift language combat promotion" http://item.jd.com/11641501.html
Swift language combat promotion-Chapter 9 game combat-Parkour Panda-2 create panda class