Skip to main content
Code correction - sorry
Source Link

I'm not really a fan of this :

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

and prefer to do the following:

        let contactcontactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
        
        switch contactcontactMask {
        // Redball and GreenBar have contacted
        case RedBallCategory | GreenBarCategory :
           score += 1       // ++ is deprecated
           scoreLabel.text = "\(score)"
    
           // Remove GreenBar
       removeFromParent(    let GreenBar = contact.bodyA.categoryBitMask == GreenBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
           GreenBar.removeFromParent
    
        // Greenball and RedBar have contacted
        case GreenBallCategory | RedBarCategory :
           score += 1       // ++ is deprecated
           scoreLabel.text = "\(score)"
    
           // Remove RedBar
       removeFromParent(    let RedBar = contact.bodyA.categoryBitMask == RedBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
           RedBar.removeFromParent
        
       default :
         //Some other contact has occurred
       }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       removeFromParent(contact.bodyA.categoryBitMask == GreenBarCategory ? contact.bodyA.node! : contact.bodyB.node!)

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       removeFromParent(contact.bodyA.categoryBitMask == RedBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

I'm not really a fan of this :

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

and prefer to do the following:

        let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
        
        switch contactMask {
        // Redball and GreenBar have contacted
        case RedBallCategory | GreenBarCategory :
           score += 1       // ++ is deprecated
           scoreLabel.text = "\(score)"
    
           // Remove GreenBar
           let GreenBar = contact.bodyA.categoryBitMask == GreenBarCategory ? contact.bodyA.node! : contact.bodyB.node!
           GreenBar.removeFromParent
    
        // Greenball and RedBar have contacted
        case GreenBallCategory | RedBarCategory :
           score += 1       // ++ is deprecated
           scoreLabel.text = "\(score)"
    
           // Remove RedBar
           let RedBar = contact.bodyA.categoryBitMask == RedBarCategory ? contact.bodyA.node! : contact.bodyB.node!
           RedBar.removeFromParent
        
       default :
         //Some other contact has occurred
       }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

Updated to use ternary condition for removeFromParent
Source Link

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       if removeFromParent(contact.bodyA.categoryBitMask == GreenBarCategory { 
          ? removeBlock(contact.bodyA.node!)
       } else {
          : removeBlock(contact.bodyB.node!)
       }

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       if removeFromParent(contact.bodyA.categoryBitMask == RedBarCategory { 
          ? removeBlock(contact.bodyA.node!)
       } else {
          : removeBlock(contact.bodyB.node!)
       }
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

You might even be able to do this:

       // Remove RedBar
       removeBlock(contact.bodyA.categoryBitMask == RedBarCategory ?? contact.bodyA.node! : contact.bodyB.node!)

but I'm not sure - it's worth trying though 😀

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       if contact.bodyA.categoryBitMask == GreenBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       if contact.bodyA.categoryBitMask == RedBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

You might even be able to do this:

       // Remove RedBar
       removeBlock(contact.bodyA.categoryBitMask == RedBarCategory ?? contact.bodyA.node! : contact.bodyB.node!)

but I'm not sure - it's worth trying though 😀

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       removeFromParent(contact.bodyA.categoryBitMask == GreenBarCategory ? contact.bodyA.node! : contact.bodyB.node!)

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       removeFromParent(contact.bodyA.categoryBitMask == RedBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

Changed from if...then to select...case. Added possibility to use ?? :
Source Link

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       if contact.bodyA.categoryBitMask == GreenBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       if contact.bodyA.categoryBitMask == RedBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

You might even be able to do this:

       // Remove RedBar
       removeBlock(contact.bodyA.categoryBitMask == RedBarCategory ?? contact.bodyA.node! : contact.bodyB.node!)

but I'm not sure - it's worth trying though 😀

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       if contact.bodyA.categoryBitMask == GreenBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       if contact.bodyA.categoryBitMask == RedBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

I'm not really a fan of this :

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

and prefer to do the following:

    let contact = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    
    switch contact {
    // Redball and GreenBar have contacted
    case RedBallCategory | GreenBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove GreenBar
       if contact.bodyA.categoryBitMask == GreenBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }

    // Greenball and RedBar have contacted
    case GreenBallCategory | RedBarCategory :
       score += 1       // ++ is deprecated
       scoreLabel.text = "\(score)"

       // Remove RedBar
       if contact.bodyA.categoryBitMask == RedBarCategory { 
           removeBlock(contact.bodyA.node!)
       } else {
           removeBlock(contact.bodyB.node!)
       }
    
   default :
     //Some other contact has occurred
   }

You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else

You might even be able to do this:

       // Remove RedBar
       removeBlock(contact.bodyA.categoryBitMask == RedBarCategory ?? contact.bodyA.node! : contact.bodyB.node!)

but I'm not sure - it's worth trying though 😀

Changed from if...then to select...case
Source Link
Loading
Source Link
Loading