我需要能够为一个精灵运行didBeginContact(),但对于另一个精灵,不要运行它(但仍会与之碰撞)。
这是我到目前为止的内容:
enum ColliderType: UInt32 {
case Player = 1
case Wall = 2
case ScreenBoundary = 3
}
屏幕边界(我不希望它运行didBeginContact())
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody!.categoryBitMask = ColliderType.ScreenBoundary.rawValue
self.physicsBody!.contactTestBitMask = ColliderType.Player.rawValue
播放器(如果与ColliderType.Wall碰撞,请运行didBeginContact())
player!.physicsBody!.categoryBitMask = ColliderType.Player.rawValue
player!.physicsBody!.contactTestBitMask = ColliderType.Wall.rawValue
player!.physicsBody!.collisionBitMask = ColliderType.Wall.rawValue
墙壁(如果与ColliderType.Player碰撞,则运行didBeginContact())
wallSprite.physicsBody!.categoryBitMask = ColliderType.Wall.rawValue
wallSprite.physicsBody!.contactTestBitMask = ColliderType.Player.rawValue
wallSprite.physicsBody!.collisionBitMask = ColliderType.Player.rawValue
希望这是有道理的,因为SpriteKit碰撞不适合我。
谢谢!
托比
最佳答案
您要做的只是在didBeginContact()方法中添加一个条件,在这种情况下,您应该添加以下内容进行检查:
func didBeginContact(contact: SKPhysicsContact) {
let other = contact.bodyA.categoryBitMask == ColliderType.Player.rawValue ? contact.bodyB : contact.bodyA
switch other.categoryBitMask {
case ColliderType.ScreenBoundary.rawValue:
println("Hit Screen!")
case ColliderType.Wall.rawValue:
println("Hit Wall!")
default:
break
}
}