Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didBegin contact not being called in Swift

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

like image 241
Will Avatar asked Jun 03 '17 02:06

Will


1 Answers

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
like image 155
Smilez Avatar answered Oct 20 '22 05:10

Smilez