O'Reilly logo

Game Development with Swift by Stephen Haney

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. In the Player.swift file, add six new properties to the Player 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required