swiftsprite-kitskspritenode

Spritekit Physics body circle of radius not registering collision


I am relatively new to Swift and Sprite kit but have ran across an issue where I have changed the physics body of an SKSpriteNode to be:

bucket50.physicsBody = SKPhysicsBody(circleOfRadius: bucket50.size.width / 4.0)

Originally it was -

bucket50.physicsBody = SKPhysicsBody(texture: bucket50.texture!, size: bucket50.texture!.size())

Since making this change the collision detection I have put in place no longer works however as far as I am concerned I have just made the physics body smaller. The ball I am sending towards the bucket does collide but the score isn't registered.

Code is also below that I a using for detection.

func didBegin(_ contact: SKPhysicsContact) {

    if contact.bodyA.node?.name == "bucket_50" {

        ball.isHidden = true
        
        //Add score for hitting bucket
        highScore = highScore + 50
        
        //Display Text
        score.text = "Score - \(highScore)"

    }

Thanks in advance


Solution

  • First you need to assign the mask value to the nodes you are going to use, then you need to use the mask category of the two nodes to compare at contact time, and when you get it you can perform an action.

    Something like this:

    let contacting: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    if contacting == bucket50.categoryBitMask | other.categoryBitMask {
        setAction
    }
    

    All within didBegin