Game Development Reference
In-Depth Information
playerNode!.physicsBody!.collisionBitMask = 0
addChild(playerNode!)
orbNode = SKSpriteNode(imageNamed: "PowerUp")
orbNode!.position = CGPoint(x: 150.0, y:
size.height - 25)
orbNode!.physicsBody =
SKPhysicsBody(circleOfRadius:
orbNode!.size.width / 2)
orbNode!.physicsBody!.dynamic = false
orbNode!.physicsBody!.categoryBitMask
= CollisionCategoryPowerUpOrbs
orbNode!.physicsBody!.collisionBitMask = 0
orbNode!.name = "POWER_UP_ORB"
addChild(orbNode!)
}
override func touchesBegan(touches: NSSet, withEvent
event: UIEvent) {
playerNode!.physicsBody!.applyImpulse(CGVectorMake(0.0,
40.0))
}
func didBeginContact(contact: SKPhysicsContact!) {
var nodeB = contact.bodyB!.node!
if nodeB.name == "POWER_UP_ORB" {
nodeB.removeFromParent()
}
}
}
Summary
In this chapter I discussed Sprite Kit's physics engine and collision detection. I began the
chapter with a discussion of the SKPhysicsBody . You then turned on the game world's
gravity, which pulled the player down the length of the scene. After that, you added a
touch responder to propel the playerNode back up into space. Finally, I closed the
chapter with a discussion on how to handle collsions.
Search WWH ::




Custom Search