Game Development Reference
In-Depth Information
After you have made all of the
SpaceMan
changes, let's move on to doing the same to
both the
Orb
and
BlackHole
classes. First change the
init()
method of the
Orb
to
take an
SKTextureAtlas
.
init(textureAtlas: SKTextureAtlas) {
let texture = textureAtlas.textureNamed("PowerUp")
super.init(texture: texture, color:
UIColor.clearColor(), size: texture.size())
physicsBody = SKPhysicsBody(circleOfRadius: size.width
/ 2)
physicsBody!.dynamic = false
physicsBody!.categoryBitMask
= CollisionCategoryPowerUpOrbs
physicsBody!.collisionBitMask = 0
name = "POWER_UP_ORB"
}
After changing the
Orb
's
init()
method, change
GameScene
's
addOrbsToFore-
ground()
to look like the following:
func addOrbsToForeground() {
var orbNodePosition =
CGPoint(x: playerNode!.position.x, y:
playerNode!.position.y + 100)
var orbXShift : CGFloat = -1.0
for i in 0...49 {
// new code to use an orb
let orbNode = Orb(textureAtlas: self.textureAtlas)
if orbNodePosition.x - (orbNode.size.width * 2) <=
0 {
orbXShift = 1.0
}
if orbNodePosition.x + orbNode.size.width >=
size.width {
orbXShift = -1.0
}
orbNodePosition.x += 40.0 * orbXShift
orbNodePosition.y += 120
orbNode.position = orbNodePosition