Player health and damage
The first custom contact logic is player damage. We will assign the player health points and take them away when damaged. The game will end when the player runs out of health. This is one of the core mechanics of our gameplay. Follow these steps to implement the health logic:
In the
Player.swift
file, add six new properties to thePlayer
class:// The player will be able to take 3 hits before game over: var health: Int = 3 // Keep track of when the player is invulnerable: var invulnerable = false // Keep track of when the player is newly damaged: var damaged = false // We will create animations to run when the player takes // damage or dies. Add these properties to store them: var damageAnimation = SKAction() var dieAnimation = SKAction() // We want to stop forward velocity if the player dies, // so we will now store forward velocity as a property: var...