检查didBeginContact中碰撞的最有效的方法

使用SpriteKit的didBeginContact方法检查冲突的最佳方法是什么? 我目前正在按照类似的方式进行检查:

 if let thisMine = nodeA as? Mine { if nodeB is Player { thisMine.explode() } } else if let thisMine = nodeB as? Mine { if nodeA is Player { thisMine.explode() } } 

我在didBeginContact方法中做了很多次,因为我有很多不同的可以相互交互的对象。 通过位掩码检查是否更有效? 另外,有没有办法减less需要基本上复制所有的代码,通过检查nodeA和nodeB作为相同的类?

使用类别bitMasks:

  func didBeginContact(contact: SKPhysicsContact) { let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask switch contactMask { case categoryBitMask.player | categoryBitMask.thisMine: print("Collision between player and thisMine") let mineNode = contact.bodyA.categoryBitMask == categoryBitMask.thisMine ? contact.bodyA.node! : contact.bodyB.node! mineNode.explode() default : //Some other contact has occurred print("Some other contact") } }