Animate the Monsters

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Build a Monster Generator | TOC | Next Steps 👉

To help distinguish your monster generators from standard monsters, you’ll create an animation component, which you’ll use to animate the monsters. First you’ll create an extension to help build the textures. Then, you’ll build out the animation component and define the different types of animations.

Creating an Animation Extension

In Load the Textures, you built a small helper method to help load textures for animation. This time around, however, you’ll create an extension method for the SKTexture class instead. Extensions are generally preferred as they tend to be more reusable than helper methods that can easily get buried within the underbelly of your code.

In the Project Navigator, and inside the Extensions group, create a new file (⌘N) using the iOS Swift Filetemplate. Name the new file SKTexture+LoadTextures.swift and replace its contents with the following:

​ ​import​ ​SpriteKit​

​ ​extension​ ​SKTexture​ {
​ ​static​ ​func​ ​loadTextures​(atlas: ​String​, prefix: ​String​,
​ startsAt: ​Int​, stopsAt: ​Int​) -> [​SKTexture​] {

​ ​var​ textureArray = [​SKTexture​]()
​ ​let​ textureAtlas = ​SKTextureAtlas​(named: atlas)
​ ​for​ i ​in​ startsAt...stopsAt {
​ ​let​ textureName = ​"​​\(​​prefix​​)\(​i​)​​"​
​…

--

--

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.