0
class GameScene: SKScene {
    
    let player = SKSpriteNode(imageNamed: "spaceship")
    
    struct PhysicsCategories {
        static let None: UInt32 = 1
        static let Player: UInt32 = 0b1
        static let Obsticles: UInt32 = 0b10
        static let Diamond: UInt32 = 0b100
    }
    
    func didBegin(_ contact: SKPhysicsContact) {
        
        var body1 = SKPhysicsBody()
        var body2 = SKPhysicsBody()
        
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            body1 = contact.bodyA
            body2 = contact.bodyB
        }
        else {
            body1 = contact.bodyB
            body2 = contact.bodyA
        }
        
        if body1.categoryBitMask == PhysicsCategories.Player && body2.categoryBitMask == PhysicsCategories.Obsticles {
            body1.node?.removeFromParent()
            body2.node?.removeFromParent()
        }
        
    }

I want to remove the diamond once it come in contact with the player and also the player when it comes in contact with the obstacle. Currently im unable to register the contact between any of them Im currently using Xcode version 9.4.1. Will the problem stay if I update the app to

Aakash Rathee
  • 523
  • 3
  • 17

1 Answers1

0

You've got a lot of things missing here (unless you've a lot of code you're not showing) :

  1. Contacts only occur between physics bodies and you don't have any
  2. Your class isn't an SKPhysicsContactDelegate and you haven't made yourself the physics contact delegate.
  3. You haven't defined which contacts you want your code to be notified for. By default, all physics bodies 'bounce off' each other (collisions) but you don't get notified when ANY bodies touch (contacts).

I've written more details here : How to Detect collision in Swift, Sprite kit

Steve Ives
  • 7,894
  • 3
  • 24
  • 55