0

I already have my categorybitmasks setup hopefully correctly but my code below should have my bullet and enemy contact and then remove each other. Nothing happens, they just collide and still stay on the screen

func didBeginContact(contact: SKPhysicsContact!) {

  var firstBody: SKPhysicsBody!
  var secondBody: SKPhysicsBody!

  if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
    firstBody = contact.bodyA
    secondBody = contact.bodyB
  }
  else {
    firstBody = contact.bodyB
    secondBody = contact.bodyA
  }

  if (firstBody.categoryBitMask & bulletCategory) != 0 &&
    (secondBody.categoryBitMask & enemyCategory) != 0 {
        destroyEnemy(bullet: firstBody.node as! SKSpriteNode, enemy: secondBody.node as! SKSpriteNode)
  }
}

func destroyEnemy(bullet: SKSpriteNode, enemy: SKSpriteNode) {
  
  bullet.removeFromParent()
  enemy.removeFromParent()
}
    enemy.physicsBody?.categoryBitMask = CollisionTypes.enemy.rawValue
    enemy.physicsBody?.contactTestBitMask = CollisionTypes.bullet.rawValue
    enemy.physicsBody?.collisionBitMask = CollisionTypes.bullet.rawValue

bullet.physicsBody?.categoryBitMask = CollisionTypes.bullet.rawValue bullet.physicsBody?.contactTestBitMask = CollisionTypes.enemy.rawValue bullet.physicsBody?.collisionBitMask = CollisionTypes.enemy.rawValue

nathandr
  • 27
  • 3
  • "I already have my categorybitmasks setup hopefully correctly" Who knows? – El Tomato Oct 12 '21 at 03:36
  • Are you getting calls at all? Did you forget to set the contact delegate maybe? – bg2b Oct 12 '21 at 10:23
  • Put a print("didBegin: entered") as your first line of `didBegin(contact:)` so that you know if any contact at all is being registered. Have you done everything else necessary for contact detection? https://stackoverflow.com/a/51041474/1430420 – Steve Ives Oct 12 '21 at 10:28
  • Contact delegate is already set. Nothing is being called back after the print. There is definitely collision between them, just not registering after contact. – nathandr Oct 12 '21 at 18:39
  • The one thing you can be sure of is that if `didBegin` isn't being called, then you haven't set up contacts correctly - it never "just doesn't work". – Steve Ives Oct 13 '21 at 10:06
  • @nathandr "Nothing is being called back after the print. " So you put a print in and that is printing something indicating that `didBegin` is being called? Maybe expand the print statement now to show what 2 nodes are being passed: `print("didBeginContact entered for \(String(describing: contact.bodyA.node!.name)) and \(String(describing: contact.bodyB.node!.name))")` – Steve Ives Oct 13 '21 at 10:10
  • i edited the post to show my categorymasks – nathandr Oct 14 '21 at 03:11

1 Answers1

0

This isn't an answer per se, but is a bit long for a comment:

In your didBegin you have:

if (firstBody.categoryBitMask & bulletCategory) != 0 &&
    (secondBody.categoryBitMask & enemyCategory) != 0 {
        destroyEnemy(bullet: firstBody.node as! SKSpriteNode, enemy: secondBody.node as! SKSpriteNode)

What happens if firstBody is the enemy and secondBody is the bullet? It would appear that the code to be run when the contact occurs won't fire. Without seeing your categoryBitMask definitions we won't know because you appear to be assigning first and second body in ascending order of the categoryBitMask value.

You could confirm if this is working with another print inside the if block.

I've never really liked this form of didBegin and I personally find this style more readable:

  func didBegin(_ contact: SKPhysicsContact) {
     print("didBeginContact entered for \(String(describing: contact.bodyA.node!.name)) and \(String(describing: contact.bodyB.node!.name))")

     let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

     switch contactMask {
     case bulletCategory | enemyCategory:
        print("bullet and enemy have contacted.")
        let bulletNode = contact.bodyA.categoryBitMask == bulletCategory ? contact.bodyA.node : contact.bodyB.node
        let enemyNode = contact.bodyA.categoryBitMask == enemyCategory ? contact.bodyA.node : contact.bodyB.node
        destroyEnemy(bullet: bulletNode, enemy: enemyNode)
     case playerCategory | enemyCategory:
        // Handle player/Enemy collisions
     default:
        print("Some other contact occurred")
     }
Steve Ives
  • 7,894
  • 3
  • 24
  • 55