Game Development Reference
In-Depth Information
Listing 9-1 . SharedConstants.swift: A File to Hold Constant Used in Multiple Classes
let CollisionCategoryPlayer : UInt32 = 0x1 << 1
let CollisionCategoryPowerUpOrbs : UInt32 = 0x1 << 2
let CollisionCategoryBlackHoles : UInt32 = 0x1 << 3
After you have the constants set up, create another new file, named SpaceMan.swift .
This is the file that will hold your refactored playerNode 's class. Once you have cre-
ated the file, copy the code in Listing 9-2 into it and save your work.
Listing 9-2 . SpaceMan.swift: The New SpaceMan Class
import Foundation
import SpriteKit
class SpaceMan: SKSpriteNode {
override init() {
let texture = SKTexture(imageNamed: "Player")
super.init(texture: texture,
color: UIColor.clearColor(),
size: texture.size())
self.physicsBody = SKPhysicsBody(circleOfRadius:
self.size.width / 2)
self.physicsBody!.dynamic = false
self.physicsBody!.linearDamping = 1.0
self.physicsBody!.allowsRotation = false
self.physicsBody!.categoryBitMask
= CollisionCategoryPlayer
self.physicsBody!.contactTestBitMask =
CollisionCategoryPowerUpOrbs
| CollisionCategoryBlackHoles
self.physicsBody!.collisionBitMask = 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
As you look at the SpaceMan class, you will see that it extends SKSpriteNode , and
all of the code related to setting up the spaceman's texture and physicsBody has been
Search WWH ::




Custom Search