didBeginContact logic OSX swift

72 views Asked by At

I am having some trouble with the logic inside of didBeginContact on an OSX game i'm creating. It is written in swift, here is what the code currently is:

   func didBeginContact(contact: SKPhysicsContact) {

    let collision: UInt32 = contact.bodyA.categoryBitMask |
        contact.bodyB.categoryBitMask

    if collision == PhysicsCategory.player | PhysicsCategory.wall {

    } else if collision ==
        PhysicsCategory.player | PhysicsCategory.box {
            player.setJump(true)

    } else if collision ==
        PhysicsCategory.player | PhysicsCategory.floor {
            player.setJump(true)
    }

    if collision ==
        PhysicsCategory.laser | PhysicsCategory.wall {

            effects = SKEmitterNode(fileNamed: "Explosion.sks")
            addChild(effects!)
            laser!.removeFromParent()
    }

   else  if collision ==
        PhysicsCategory.laser | PhysicsCategory.floor {
            effects = SKEmitterNode(fileNamed: "Explosion.sks")
            addChild(effects!)
             laser!.removeFromParent()
    }

I feel like this mess of if statements is horribly inefficient, but I have no idea how to convert this to a switch statement or if that would even be more efficient. I've tried removing the 'else' and just have a series of 'if' statements but then I have even more bugs when collisions happen (boxes falling through walls ect..) Any advice would be appretiated

1

There are 1 answers

0
C. Greene On BEST ANSWER

In case anyone stumbles across this, here is how I resolved it.

let collision: UInt32 = contact.bodyA.categoryBitMask | 

 contact.bodyB.categoryBitMask

    switch collision{

    case PhysicsCategory.player | PhysicsCategory.floor:
         playerObj.setJump(true)

    case  PhysicsCategory.player | PhysicsCategory.box:
        playerObj.setJump(true)

    case PhysicsCategory.player | PhysicsCategory.wall:
        return
     // ect...
     default:
        return
}