Game Development Reference
In-Depth Information
Let's see how to set up the collision detection properties using the two nodes. First let's
start with the
playerNode
. The following three lines set up the player's collision prop-
erties:
playerNode!.physicsBody!.categoryBitMask
= CollisionCategoryPlayer
playerNode!.physicsBody!.contactTestBitMask
= CollisionCategoryPowerUpOrbs
playerNode!.physicsBody!.collisionBitMask = 0
The first line of code associates the
playerNode.physicsBody
's category bit mask
to the
CollisionCategoryPlayer
. The second line tells Sprite Kit that whenever
your physics body comes into contact with another physics body belonging to the cat-
egory
CollisionCategoryPowerUpOrbs
, you want to be notified. The final line,
setting the
playerNode.physicsBody
's
collisionBitMask
to 0, tells Sprite Kit
not to handle collisions for you. The game is going to be doing this itself in the
didBe-
ginContact()
method. Go ahead and add these three lines to the
GameS-
cene.init(size: CGSize)
method immediately after this line:
playerNode!.physicsBody.allowsRotation = false
Next, let's move on to configuring the
orbNode
's
phsyicsBody
. Setting up the
or-
bNode
's physics body is even easier than setting up the
playerNode
and can be done
with only two lines of code.
orbNode!.physicsBody!.categoryBitMask
= CollisionCategoryPowerUpOrbs
orbNode!.physicsBody!.collisionBitMask = 0
The first line associates the
orbNode
's physics body to the category
CollisionCat-
egoryPowerUpOrbs
, and the second line, just like when configuring the player, is set
to 0 because you are going to handle collisions yourself. There is one thing to note here.
When configuring the orb node, you are not setting a
contactTestBitMask
. You are
not doing this because it is not necessary. You will be notified of the contact because you
already set this up in the
playerNode
. Add these two lines to the
GameS-
cene.init(size: CGSize)
method immediately before adding the
orbNode
to
the scene: