Trouble with bit masks in SpriteKit SWIFT

60 views Asked by At

In my game I have circles that have different colors. They should only collide with boundaries that are not of their color. To do that I at first assign the normal circle a category bit mask.

    struct CollisionCategoryBitmask {
    static let monster: UInt32 = 00000001
    static let picker: UInt32 = 00000010
    static let bound: UInt32 = 00000100
    static let circle: UInt32 = 00001000
    static let yellow: UInt32 = 00010000
    static let brown: UInt32 = 00100000
    static let cyan: UInt32 = 01000000
    static let magenta: UInt32 = 10000000
}

physicsBody?.categoryBitMask  = CollisionCategoryBitmask.circle

The boundaries have the following masks:

boundary.physicsBody?.categoryBitMask = CollisionCategoryBitmask.bound
boundary.physicsBody?.collisionBitMask = 11101111

After the circle collides with a picker it gets a bit mask that should make it stop colliding with the boundary however it doesn't.

self.circles[i].fillColor = .yellow
                    self.circles[i].physicsBody?.categoryBitMask = 00010000
                    self.circles[i].physicsBody?.collisionBitMask = 11111011

This code is executed when the circle collide with the picker. The values in the struct like cyan and magenta are each for a state of the circle where it doesn't collide with a boundary of that color. Where is my mistake?

1

There are 1 answers

0
s1ddok On

Just add "0b" in the beginning of every literal. You don't use binary notation right now.

Read more about how bit masks work.