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:
In Xcode, open
GameScene.swift
and locate thedidBegin
function, where we set our physics contact logic.We need to call the
explode
function on ourCrate
class any time the player runs into aCrate
. 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) }
Before we can award health, we need to add a new property to the
Player
class to set a maximum amount of health. OpenPlayer.swift
and add a new property...