I have two nodes, and I want to detect a collision between them, but for some reason
func didBegin(_ contact: SKPhysicsContact) {
is not being called.
It is a ball and a paddle:
ball.physicsBody?.categoryBitMask = 2
ball.physicsBody?.contactTestBitMask = 2
ball.physicsBody?.isDynamic = true
main.physicsBody?.categoryBitMask = 1
main.physicsBody?.contactTestBitMask = 1
main.physicsBody?.isDynamic = false
_
func didBegin(_ contact: SKPhysicsContact) {
print("Collision") //Obviously this will be better in future, but I need to detect collision for a start
}
When the ball moves around, it DOES bounce off the paddle, but I need to be able to detect that.
Thanks
check these things:
Make sure you set the physics world contact delegate to itself class GameScene: SKScene, SKPhysicsContactDelegate{
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
self.backgroundColor = .white
}
if you had specified a physics body eg:
box.physicsBody = SKPhysicsBody(rectangleOf: CGSize)
make sure it's definitely unwrapped (! not ?)
box.physicsBody!.affectedByGravity = true
Make the physics bodies dynamic:
box.physicsBody!.isDynamic = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With