Game Development Reference
In-Depth Information
override func touchesBegan(touches: NSSet, withEvent
event: UIEvent) {
playerNode!.physicsBody!.applyImpulse(CGVectorMake(0.0,
40.0))
}
}
Adding
Collision
Detection
to
Your
SKNode
At this point, the game has a playerNode that responds to gravity and impulses; it's
now time to add another node that your player can interact with. In this section, you are
going to add another sprite that represents an orb in your game. You will begin by adding
the orb and positioning it above the player. After that, you will modify the orb's properties
so the player and orb interact naturally in the game. And finally, you will add code that
will detect collisions between the player and the orb and remove the orb from the scene
when they collide.
Adding a Node to Collide Into
Let's get started. The first thing you are going to do is add the orb sprite to the GameS-
cene at a position a little to the left and above the player. The image you are going to use
for the orb is in the sprites.atlas collection and is named PowerUp . There is noth-
ing special about adding this sprite—you do so just like you added the player. First add
the declaration of the new node to the GameScene immediately following the declara-
tion of the playerNode .
let orbNode : SKSpriteNode?
After adding the declaration of the orbNode , add this code to the end of the GameS-
cene.init(size: CGSize) method:
orbNode = SKSpriteNode(imageNamed: "PowerUp")
orbNode!.position = CGPoint(x: 150.0, y: size.height - 25)
orbNode!.physicsBody =
SKPhysicsBody(circleOfRadius: orbNode!.size.width / 2)
Search WWH ::




Custom Search