Game Development Reference
In-Depth Information
Adding Bit Masks to Your SKPhysicsBody
Everything is wired up to receive contact notifications, so the next thing you need to do is
tell Sprite Kit which objects you are interested in receiving contact notifications. You do
this using a concept called bit masks .
The SKPhysicsBody has three bit mask properties you can use to define the way your
physics body interacts with other physics bodies in a game world: colli-
sionBitMask , categoryBitMask , and contactTestBitMask . Each of these
properties is described in Table 3-1 .
Table 3-1 . The SKPhysicsBody's Three Bit Mask Properties
Method
Purpose
Defines the collision categories that your SKPhysicsBody will bump in-
to. All other physics bodies will be passed through.
1. collisionBitMask
2. categoryBitMask
Defines the collision categories a physics body belongs.
Determines which categories this physics body makes contact with. An ex-
ample, used in your game, would be coming in contact with the orb. You
want Sprite Kit to tell you when the playerNode comes into contact with
the orbNode .
3. con-
tactTestBitMask
At the moment, the app has only two nodes: a player and an orb. This makes it easy to cat-
egorize them. You can put the player into the category CollisionCategoryPlayer
and all the orb nodes (there is only one at the moment) into a category named Colli-
sionCategoryPowerUpOrbs . These two category bit masks are defined here:
let CollisionCategoryPlayer : UInt32 = 0x1 << 1
let CollisionCategoryPowerUpOrbs : UInt32 = 0x1 << 2
Here are two bit masks, CollisionCategoryPlayer and CollisionCat-
egoryPowerUpOrbs , each of which is an unsigned 32-bit integer. This is an important
thing to note because collision bit masks are 32 bits, and you can have only 32 unique cat-
egories. Go ahead and add these two lines to the GameScene immediately following the
declaration of the orbNode and before the first init() method:
let CollisionCategoryPlayer : UInt32 = 0x1 << 1
let CollisionCategoryPowerUpOrbs : UInt32 = 0x1 << 2
Search WWH ::




Custom Search