2017-08-04 81 views
0

我有兩個我想碰撞的實體。我有一個結構,以保持不同的物理類的軌跡:collisionBitMask值在SpriteKit中不起作用

struct PhysicsCategory { 
    static let Player: Int32 = 0x1 << 1 
    static let Obstacle: Int32 = 0x1 << 2 
    static let Ground: Int32 = 0x1 << 3 
} 

我希望我的球員節點與我的障礙物碰撞的節點。這裏是我的球員節點的物理:

self.physicsBody = SKPhysicsBody(rectangleOf: (self.size)) 
    self.physicsBody?.isDynamic = true 
    self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Player) 
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle) 
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground) 

這裏是我的障礙節點的物理

self.physicsBody = SKPhysicsBody(rectangleOf: self.size) 
    self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Obstacle) 
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) 
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground) 
    self.physicsBody?.isDynamic = true 
    self.physicsBody?.velocity = CGVector(dx: -240, dy: 0) 
    self.physicsBody?.linearDamping = 0 
    self.physicsBody?.friction = 0 

當他們穿越的路徑,他們只是通過彼此。但是,它們都與地面碰撞。我究竟做錯了什麼?

+1

該方案的基本mation告訴你如果你寫x = 1和x = 2之後,你的x是2 –

回答

2

當您第二次設置時,您正在覆蓋collisionBitMask。您應該設置一個OR位掩碼,其符號爲|

更換

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle) 
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground) 

有:

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle) | UInt32(PhysicsCategory.Ground) 

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) 
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground) 

有:

​​