Add the Player’s Heads-Up Display

Apple Game Frameworks and Technologies — by Tammy Coron (136 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Use the Action Editor to Add Actions | TOC | Build Better On-Screen C ontrols 👉

What better way to keep a player informed than with a heads-up display (HUD). With a HUD, you’re able to display important information about the game in a way that is always visible to the player without “getting in the way” of the gameplay. At the moment, the player has no way of knowing how many keys Val has in her pocket or how much treasure she’s acquired.

In Adding New Functionality to the Player Class, you added two properties — keys and treasure — to help keep track of these important things. You’ll use these properties with your new HUD.

A lot of the code you’re about to write should be familiar to you since you’ve seen similar examples in previous chapters. So, to keep things moving along, the code explanations will be kept to a minimum unless there’s a new concept to learn.

Building the Player HUD

In the Project Navigator, select the Player.swift file and add three new properties to the Player class, placing them just above the keys property:

​ ​var​ hud = ​SKNode​()
​ ​private​ ​let​ treasureLabel = ​SKLabelNode​(fontNamed: ​"AvenirNext-Bold"​)
​ ​private​ ​let​ keysLabel = ​SKLabelNode​(fontNamed: ​"AvenirNext-Bold"​)

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.