简体   繁体   中英

Trouble with bit masks in SpriteKit SWIFT

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?

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

Read more about how bit masks work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM