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 ...
Get Game Development with Swift 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.