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 ...
Get Swift Game Development - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.