Add a New Collectible Component

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Set Up the Player State Machine | TOC | Configure the Physics Bodies 👉

You first learned about components in Create Your First Component. With components, the focus is on what a “thing” does versus what it is. You might believe that this focus lets you off the hook for thinking about what things are, but that isn’t the case; you still need to consider how you’ll be using your components.

In Val’s Revenge, there are three kinds of collectible items (types):

images/UsingStatesAndStateMachines/collectibles.png

Each type serves a useful purpose:

  • Key: Collect keys to unlock dungeon doors
  • Food: Collect food to increase Val’s health (up to her maximum)
  • Treasure: Collect treasure for bragging rights

Similar to what you did in Creating an Animation Component, you’ll create a single file to hold the collectible component and the corresponding collectible struct that defines the collectible properties.

Creating the Collectible Struct

In the Components group, create a new file (⌘N) using the iOS Swift File template. Name the new file CollectibleComponent.swift and replace the contents…

--

--

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.