Create Your First Entity

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Create Your First Component | TOC | Build a Monster Generator 👉

An entity, as it relates to the Entity-Component architecture, is any game object within your game. This can include things like the player, the monsters, the collectibles, and even the projectiles — but typically, not something like the background.

With GameplayKit, you can either use a generic GKEntity object or create a new GKEntity subclass. For Val’s Revenge, you’ll create a new subclass.

In keeping with your new project organization, create a new group (⌥⌘N) above the Components group and name this new group Entities.

Although the Player class isn’t technically an entity (it’s an SKSpriteNode), drag the Player.swift file into the newly created Entities group. By placing the Player.swift file into the Entities group, you are keeping up with your overall project organization.

Inside the Entities group, create a new file (⌘N) using the iOS Swift File template. Name this new file MonsterEntity.swift and replace its contents with the following code:

​ ​import​ ​SpriteKit​
​ ​import​ ​GameplayKit​

​ ​class​ ​MonsterEntity​: ​GKEntity​ {

​ ​init​(monsterType: ​String​) {
​ ​super​.​init​()

​ }

​ ​required​ ​init​?(coder: ​NSCoder​) {
​ ​super​.​init​(coder:coder)
​ }
​ }

--

--

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.