Das Problem, zu dem ich renne ist, dass der Code, den ich erstellt habe, um den Kontakt zu erkennen, nicht funktioniert. Ich wollte, dass es funktioniert, wenn die Ballkategorie die Hinderniskategorie berührt. Was ist falsch an meinem Code? Vielen Dank, jede Hilfe wird geschätzt!Warum erkennt mein Code niemals einen Kontakt mit meinem SKPhysicsBody?
Heres, wo ich die Kategorien definieren:
let ballCategory : UInt32 = 0x1 << 1
let obstacleCategory : UInt32 = 0x1 << 2
Heres, wo ich die Physik Aspekt meiner Ball zu erstellen:
leftBall.physicsBody = SKPhysicsBody(rectangleOf: leftBall.size)
leftBall.physicsBody?.categoryBitMask = ballCategory
leftBall.physicsBody?.contactTestBitMask = obstacleCategory
leftBall.physicsBody?.affectedByGravity = false
leftBall.physicsBody?.isDynamic = false
leftBall.physicsBody?.collisionBitMask = 0
Heres, wo ich die Physik Aspekt eines Hindernisses zu erstellen:
obstacleSquare.physicsBody = SKPhysicsBody(rectangleOf: obstacleSquare.size)
obstacleSquare.physicsBody?.categoryBitMask = obstacleCategory
obstacleSquare.physicsBody?.contactTestBitMask = ballCategory
obstacleSquare.physicsBody?.affectedByGravity = false
obstacleSquare.physicsBody?.isDynamic = false
obstacleSquare.physicsBody?.collisionBitMask = 1
Und heres meine Kollisionserkennung Funktion:
func didBegin(_ contact: SKPhysicsContact) {
print("CONTACT")
if contact.bodyA.categoryBitMask == obstacleCategory {
print("CONTACT")
gameOver()
}
if contact.bodyB.categoryBitMask == obstacleCategory {
print("CONTACT")
gameOver()
}
if contact.bodyA.categoryBitMask == ballCategory {
print("CONTACT")
gameOver()
}
if contact.bodyB.categoryBitMask == ballCategory {
print("CONTACT")
gameOver()
}
}
Vielen Dank das hat funktioniert – Justin