0
votes

Basically I have the ground, the player (raymond), and coins. When the player touches the ground nothing should happen, game continues as normal. When the player comes in contact with the coin, I want it to print to console "coin contact with player".

enum ColliderType: UInt32 {
    case Raymond = 1
    case Object = 2
    case Coin = 3
}

Raymonds physics

raymond.physicsBody = SKPhysicsBody(circleOfRadius: raymondTexture.size().height/2)
    raymond.physicsBody!.dynamic = true

    raymond.physicsBody!.categoryBitMask = ColliderType.Raymond.rawValue
    raymond.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
    raymond.physicsBody?.collisionBitMask = ColliderType.Object.rawValue

Coins Physics

        coin.physicsBody = SKPhysicsBody(circleOfRadius: raymondTexture.size().height/2)
    coin.physicsBody!.dynamic = true

    coin.physicsBody!.categoryBitMask = ColliderType.Coin.rawValue
    coin.physicsBody?.contactTestBitMask = ColliderType.Raymond.rawValue
    coin.physicsBody?.collisionBitMask = ColliderType.Object.rawValue

Ground Physics if you need

        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
    ground.physicsBody!.dynamic = false
    ground.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    ground.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
    ground.physicsBody?.collisionBitMask = ColliderType.Object.rawValue

Heres the contact function I have, I know its wrong and I need help with how to detect the coin and raymond touching.

    func didBeginContact(contact: SKPhysicsContact) {
    print("coin contact with player")

}

Thanks in advance.

1
Can you add more of your code, preferably when you state the physicsOren Edrich
@OrenEdrich done.Sav Celentano
you are not using bitmasks correctly, 3 means 00000000000000000000000000000011 which really means Object & Raym0nd. You need to work in powers of 2, so the next viable number is 4 (1,2,4,8,16,32) If you want to make it readable, then use (1 << 2) for your coin (1 << 0) for your object, and (1 << 1) for your RaymondKnight0fDragon

1 Answers

1
votes

There really is several ways of checking this, Here are 2 of the most basics ways to get you started. The first checks name for contact, and the second checks the CategoryBitMask. It is worth noting that if your PhysicsBodies bitmasks are not set properly contact may never be reported between 2 objects.

Edit Make sure the Scene conforms to SKPhysicsContactDelegate

class GameScene: SKScene, SKPhysicsContactDelegate

...

func didBegin(_ contact: SKPhysicsContact) {

    let contactAName = contact.bodyA.node?.name
    let contactBName = contact.bodyB.node?.name

    if (contactAName == "raymond") || (contactBName == "raymond") {

         if (contactAName == "coin") || (contactBName == "coin") {
             print("coin contact with player")
             return
        }
    }

    //or

    if contact.bodyA.categoryBitMask == ColliderType.Coin || contact.bodyB.categoryBitMask == ColliderType.Coin {

        if contact.bodyA.categoryBitMask == ColliderType.Raymond || contact.bodyB.categoryBitMask == ColliderType.Raymond {
            print("coin contact with player")
            return
        }
    }
}