我试图不通过碰撞位掩码与穴居人和其他图像联系,但我的穴居人会击中一切。
func addCaveManBitMasks(){
caveManNode.physicsBody?.categoryBitMask = PhysicsCategory.caveman
caveManNode.physicsBody?.contactTestBitMask = PhysicsCategory.tri | PhysicsCategory.trex | PhysicsCategory.newblock | PhysicsCategory.fireBall | PhysicsCategory.waterblock | PhysicsCategory.secondwaterblock | PhysicsCategory.star
caveManNode.physicsBody?.collisionBitMask = PhysicsCategory.newblock
}
func addTriBitMasks(){
triImage.physicsBody?.categoryBitMask = PhysicsCategory.tri
triImage.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
triImage.physicsBody?.collisionBitMask = 0
}
func addRexBitMasks(){
tRexImage.physicsBody?.categoryBitMask = PhysicsCategory.trex
tRexImage.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
tRexImage.physicsBody?.collisionBitMask = 0
}
func addNewBlockManBitMasks(){
newBlockImageNode.physicsBody?.categoryBitMask = PhysicsCategory.newblock
newBlockImageNode.physicsBody?.contactTestBitMask = PhysicsCategory.caveman | PhysicsCategory.steg
newBlockImageNode.physicsBody?.collisionBitMask = PhysicsCategory.caveman
}
func addFireBallBitMasks(){
fireBall.physicsBody?.categoryBitMask = PhysicsCategory.fireBall
fireBall.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
fireBall.physicsBody?.collisionBitMask = 0
}
func addStarBitMasks(){
onScreenStar.physicsBody?.categoryBitMask = PhysicsCategory.star
onScreenStar.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
onScreenStar.physicsBody?.collisionBitMask = 0
}
struct PhysicsCategory {
static let caveman: UInt32 = 0x1 << 0
static let tri: UInt32 = 0x1 << 1
static let trex: UInt32 = 0x1 << 2
static let fireBall: UInt32 = 0x1 << 3
static let steg: UInt32 = 0x1 << 4
static let ptero: UInt32 = 0x1 << 5
static let waterblock: UInt32 = 0x1 << 6
static let secondwaterblock: UInt32 = 0x1 << 7
static let newblock: UInt32 = 0x1 << 8
static let star: UInt32 = 0x1 << 9
static let food: UInt32 = 0x1 << 10
}
tri,rex,fireball和newblock彼此之间没有联系,这是完美的,但我也需要穴居人做同样的事情。穴居人只应该与新的碰撞。我非常困惑,非常感谢解释
答案 0 :(得分:0)
这是因为您将所有其他对象collisionBitMask
设置为与穴居人相同。将这些其他对象的碰撞位掩码更改为您尚未在另一个节点上使用的位掩码,并且您将会很好。
答案 1 :(得分:0)
要让tRex,Tri和火球与穴居人碰撞(除了穴居人之外什么都没有),你需要:
tRexImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman
triImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman
firBallImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman
如果caveMan应该与上面的三个对象碰撞,那么你需要新的块:
let enemyCategory = PhysicsCategory.trex |PhysicsCategory.tri |PhysicsCategory.fireBall
caveManNode.physicsBody?.collisionBitMask = PhysicsCategory.newblock | enemyCategory
如果trex
,tri
和fireBall
也应该相互冲突,那么您需要:
tRexImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory
triImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory
firBallImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory
以下是一个示例Xcode Sprite-kit项目,试图证明其用途 - Attack button in SpriteKit
使用上述项目中的checkPhysics()函数 - 它将列出哪些对象碰撞以及哪些对象生成联系人。一旦创建了所有物理实体和contactTest / collision位掩码,就调用它。