Assigning categories to game objects
Now that we have the physics categories, we need to go back through our existing game objects and assign the categories to the physics bodies. We will start with the Player class.
The player
Open Player.swift and add the following code at the bottom of the init function:
self.physicsBody?.categoryBitMask =
PhysicsCategory.penguin.rawValue
self.physicsBody?.contactTestBitMask =
PhysicsCategory.enemy.rawValue |
PhysicsCategory.ground.rawValue |
PhysicsCategory.powerup.rawValue |
PhysicsCategory.coin.rawValue
self.physicsBody?.collisionBitMask =
PhysicsCategory.ground.rawValue
We assigned the penguin physics category to the Player physics body, and used the contactTestBitMask property to set up contact logic tests with enemies, the ground, Power-ups, and coins. We used the collisionBitMask to make the penguin only bounce off the ground while gliding through other game objects.
Also...