0
class GameScene: SKScene, SKPhysicsContactDelegate {
    func didBegin(_ contact: SKPhysicsContact) {
        let contactA:SKPhysicsBody = contact.bodyA
        let contactB:SKPhysicsBody = contact.bodyB
        if contactA.contactTestBitMask == 1 && contactB.contactTestBitMask == 2 {
            jumpButtonIsReady = true
            print("player landed on ground1")
        }
 if contactA.contactTestBitMask == 2 && contactB.contactTestBitMask == 1 {
        jumpButtonIsReady = true
       
        print("player landed on ground1")
    
    }    }   
}

I have used scene editor to set the contactTestBitMask of player to be 1 and the ground to be 2. they are both set to dynamic is true. But the contact is not registering. Thanks for any help.

  • What are the categoryBitMasks for both player and ground? The value for contactTestBitMasks are the categoryBitMasks for which you want to test the contacts for. – JohnL Apr 18 '21 at 10:58
  • Their CategoryBitMasks are the same as their contacts bit mask 1 and 2. – Greg McKenna Apr 18 '21 at 12:19
  • The player categoryBitMask is not the same as its contactTestBitMask, correct? – JohnL Apr 18 '21 at 12:29
  • No, they are the same. The player contact and category bit mask is 1 and the grounds contact and category bit mask is 2. – Greg McKenna Apr 18 '21 at 12:32
  • Another issue you need to deal with is contactA can be the player and contactB can be the ground, but also contactA can be the ground and contactB the player. Physics detection does not guarantee what node is what contact. Currently you are only testing for one scenario. – JohnL Apr 18 '21 at 12:33
  • I have just edited my answer and tested both scenarios but it still does not work. – Greg McKenna Apr 18 '21 at 12:38
  • It won't work until you've set the category and contact bitmasks correctly. Again, the value for contactTestBitMasks are the categoryBitMasks for which you want to test the contacts for. Please read [this](https://stackoverflow.com/questions/31109659/how-does-collisionbitmask-work-swift-spritekit). – JohnL Apr 18 '21 at 12:42
  • I have read it a few times and changed my contact test bit masks but It still doesn't register the contacts. – Greg McKenna Apr 18 '21 at 12:55

1 Answers1

1

Likely you are missing the physicsWorld.contactDelegate = self in the scene initialization. Just conforming to SKPhysicsContactDelegate is not sufficient.

If you verify that that's OK, then check that the physics bodies are correct by setting the view's showsPhysics to true.

bg2b
  • 1,939
  • 2
  • 11
  • 18