SKPhysicsBody avoid collision Swift/SpriteKit

后端 未结 1 1556

I have 3 SKSpriteNodes in my Scene. One bird, one coin and a border around the scene. I don\'t want the coin

相关标签:
1条回答
  • 2020-12-20 09:05

    The bitmask is on 32 bits. Declaring them like you did corresponds to :

    enum CollisionType:UInt32{
        case Bird = 1 // 00000000000000000000000000000001
        case Coin = 2 // 00000000000000000000000000000010
        case Border = 3 // 00000000000000000000000000000011
    }
    

    What you want to do is to set your border value to 4. In order to have the following bitmask instead :

    enum CollisionType:UInt32{
        case Bird = 1 // 00000000000000000000000000000001
        case Coin = 2 // 00000000000000000000000000000010
        case Border = 4 // 00000000000000000000000000000100
    }
    

    Keep in mind that you'll have to follow the same for next bitmask : 8, 16, ... an so on.

    Edit :

    Also, you might want to use a struct instead of an enum and use another syntax to get it easier (it's not mandatory, just a matter of preference) :

    struct PhysicsCategory {
        static let None       : UInt32 = 0
        static let All        : UInt32 = UInt32.max
        static let Bird       : UInt32 = 0b1       // 1
        static let Coin       : UInt32 = 0b10      // 2
        static let Border     : UInt32 = 0b100     // 4
    }
    

    That you could use like this :

    bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
    bird.physicsBody!.collisionBitMask = PhysicsCategory.Border
    
    coin.physicsBody!.categoryBitMask = PhysicsCategory.Coin
    coin.physicsBody!.collisionBitMask = PhysicsCategory.Border
    
    0 讨论(0)
提交回复
热议问题