Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a heads-up display


Our game needs a HUD to show the player's current health and coin score. We can use hearts to indicate health – like classic games in the past – and draw text to the screen with SKLabelNode to display the number of coins collected.

We will attach the HUD to the scene itself, instead of to the world node, since it does not move as the player flies forward. We do not want to block the player's vision of upcoming obstacles to the right, so we will place the HUD elements in the top left corner of the screen.

When we are finished, our HUD will look like this (after the player collects 110 coins and sustains one point of damage):

To implement the HUD, follow these steps:

  1. First, we need to add the HUD art assets into the game. In the asset pack, find the HUD.atlas texture atlas and add it to your project.

  2. Next, we will create a HUD class to handle all of the HUD logic. Add a new Swift file to your project, HUD.swift, and add the following code to begin work on the HUD class...