categoryBitMask返回UInt32.max

时间:2016-08-28 22:38:19

标签: ios swift sprite-kit skphysicsbody

我正在制作一个有各种碰撞的游戏。我创建了一个Enumeration如下:

enum CollisionCategories: UInt32 {
    case localPlayer = 1
    case ball = 2
    case remotePlayer = 4
    case scene = 8
    case goal = 16
}

然后我创建了一些类别变量:

let localPlayerCategory: UInt32 = CollisionCategories.localPlayer.rawValue
let ballCategory: UInt32 =  CollisionCategories.ball.rawValue
let remotePlayerCategory: UInt32 =  CollisionCategories.remotePlayer.rawValue
let sceneCategory: UInt32 = CollisionCategories.scene.rawValue
let goalCategory: UInt32 =  CollisionCategories.goal.rawValue

这是我打印信息的地方

func didBeginContact(contact: SKPhysicsContact) {
    print("Body A: " + String(contact.bodyA.categoryBitMask))
    print("Body B: " + String (contact.bodyB.categoryBitMask) + "\n")

}

然而,当球门和球碰撞时,它会返回以下内容:

Body A: 4294967295
Body B: 2

身体B是正确的球类;然而,身体A显然是关闭的。以下是我创建目标节点的方法:

let goal2 = self.newGoal()
goal2.zRotation = CGFloat(M_PI)
goal2.position = CGPoint(x: (gameFrame!.frame.width / 2), y: 0)
let bottom2 = SKPhysicsBody(edgeFromPoint: CGPoint(x: -(goal2.frame.width), y:-(goal2.frame.height / 2)), toPoint: CGPoint(x: 0, y:-(goal2.frame.height / 2)))
bottom2.categoryBitMask = sceneCategory
let top2 = SKPhysicsBody(edgeFromPoint: CGPoint(x: -(goal2.frame.width), y:(goal2.frame.height / 2)), toPoint: CGPoint(x: 0, y:(goal2.frame.height / 2)))
top2.categoryBitMask = sceneCategory
let back2 = SKPhysicsBody(edgeFromPoint: CGPoint(x: -(goal2.frame.width), y:(goal2.frame.height / 2)), toPoint: CGPoint(x: -(goal2.frame.width), y:-(goal2.frame.height / 2)))
back2.categoryBitMask = goalCategory
goal2.physicsBody = SKPhysicsBody(bodies: [bottom2,top2,back2])
//goal2.physicsBody!.categoryBitMask = goalCategory
goal2.physicsBody!.usesPreciseCollisionDetection = true
goal2.physicsBody!.affectedByGravity = false
goal2.physicsBody!.dynamic = false

正如你在上面评论的那一行中所看到的,我也尝试将整个物理主体设置为目标,而不仅仅是查看是否导致问题的部分。事实并非如此。

以下是我创建球的物理属性的方法:

ball.physicsBody!.collisionBitMask = localPlayerCategory | sceneCategory | goalCategory
ball.physicsBody!.categoryBitMask = ballCategory
ball.physicsBody!.contactTestBitMask = goalCategory

那么,任何人都可以解释为什么他们认为bodyA的categoryBitMask正在返回max UInt32吗?请注意,除了球击中球门之外的每次碰撞都可以。

此外,快速提问:有谁知道bodyA和bodyB是如何被选中的?为什么哪个是哪个?谢谢!

1 个答案:

答案 0 :(得分:0)

根据https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/#//apple_ref/occ/clm/SKPhysicsBody/bodyWithBodies:忽略所用主体的属性。只有他们的形状被用来构建新的物理体。这意味着各个部分不能拥有自己的srandom(234234132)。我将不得不使用一个单独的节点来获得我想要的结果。