collisionBitMask 值在 SpriteKit 中不起作用
collisionBitMask values not working in SpriteKit
我有两个实体想要相互碰撞。我有一个结构来跟踪不同的物理类别:
struct PhysicsCategory {
static let Player: Int32 = 0x1 << 1
static let Obstacle: Int32 = 0x1 << 2
static let Ground: Int32 = 0x1 << 3
}
我希望我的 Player 节点与我的 Obstacle 节点发生碰撞。这是我的玩家节点的物理特性:
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)
这是我的 Obstacle 节点的物理特性
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
当他们相交时,他们只是擦肩而过。但是,它们都正确地与地面碰撞。我做错了什么?
第二次设置时,您正在覆盖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)
与:
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) | UInt32(PhysicsCategory.Ground)
我有两个实体想要相互碰撞。我有一个结构来跟踪不同的物理类别:
struct PhysicsCategory {
static let Player: Int32 = 0x1 << 1
static let Obstacle: Int32 = 0x1 << 2
static let Ground: Int32 = 0x1 << 3
}
我希望我的 Player 节点与我的 Obstacle 节点发生碰撞。这是我的玩家节点的物理特性:
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)
这是我的 Obstacle 节点的物理特性
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
当他们相交时,他们只是擦肩而过。但是,它们都正确地与地面碰撞。我做错了什么?
第二次设置时,您正在覆盖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)
与:
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) | UInt32(PhysicsCategory.Ground)