Wiring up crate contact events

Now we can fire custom logic whenever the player runs into a crate. We will place our particle effect and award health or coins. Follow these steps to wire up the contact event:

  1. In Xcode, open GameScene.swift and locate the didBegin function, where we set our physics contact logic.
  2. We need to call the explode function on our Crate class any time the player runs into a Crate. Add a new case for crate contact below the star power-up contact case, as shown here (new code in bold):
            case PhysicsCategory.powerup.rawValue: 
                player.starPower() 
            case PhysicsCategory.crate.rawValue: 
                if let crate = otherBody.node as? Crate { 
                    // Call the explode function with a reference 
                    // to the GameScene: 
                    crate.explode(gameScene: self) 
                } 
  3. Before ...

Get Swift Game Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.