Table Of Contentwww.it-ebooks.info
www.it-ebooks.info
iOS Game Development Cookbook
Jonathon Manning and Paris Buttfield-Addison
www.it-ebooks.info
iOS Game Development Cookbook
by Jonathon Manning and Paris Buttfield-Addison
Copyright © 2014 Jonathon Manning and Paris Buttfield-Addison. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Rachel Roumeliotis Indexer: Ellen Troutman-Zaig
Production Editor: Melanie Yarbrough Cover Designer: Karen Montgomery
Copyeditor: Rachel Head Interior Designer: David Futato
Proofreader: Jasmine Kwityn Illustrator: Rebecca Demarest
April 2014: First Edition
Revision History for the First Edition:
2014-04-09: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449368760 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. iOS Game Development Cookbook, the image of a queen triggerfish, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36876-0
[LSI]
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Laying Out a Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1. Laying Out Your Engine 1
1.2. Creating an Inheritance-Based Game Layout 2
1.3. Creating a Component-Based Game Layout 4
1.4. Calculating Delta Times 7
1.5. Detecting When the User Enters and Exits Your Game 8
1.6. Updating Based on a Timer 10
1.7. Updating Based on When the Screen Updates 11
1.8. Pausing a Game 13
1.9. Calculating Time Elapsed Since the Game Start 13
1.10. Working with Blocks 14
1.11. Writing a Method That Calls a Block 18
1.12. Working with Operation Queues 19
1.13. Performing a Task in the Future 21
1.14. Storing Blocks in Objects 22
1.15. Using a Timer 25
1.16. Making Operations Depend on Each Other 26
1.17. Filtering an Array with Blocks 27
1.18. Loading New Assets During Gameplay 28
1.19. Adding Unit Tests to Your Game 29
1.20. 2D Grids 32
2. Views and Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.1. Working with Storyboards 38
2.2. Creating View Controllers 43
2.3. Using Segues to Move Between Screens 50
2.4. Using Constraints to Lay Out Views 53
iii
www.it-ebooks.info
2.5. Adding Images to Your Project 55
2.6. Slicing Images for Use in Buttons 56
2.7. Using UI Dynamics to Make Animated Views 58
2.8. Moving an Image with Core Animation 60
2.9. Rotating an Image 62
2.10. Animating a Popping Effect on a View 63
2.11. Theming UI Elements with UIAppearance 65
2.12. Rotating a UIView in 3D 66
2.13. Overlaying Menus on Top of Game Content 68
2.14. Designing Effective Game Menus 69
3. Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.1. Detecting When a View Is Touched 72
3.2. Responding to Tap Gestures 73
3.3. Dragging an Image Around the Screen 74
3.4. Detecting Rotation Gestures 76
3.5. Detecting Pinching Gestures 78
3.6. Creating Custom Gestures 79
3.7. Receiving Touches in Custom Areas of a View 84
3.8. Detecting Shakes 84
3.9. Detecting Device Tilt 85
3.10. Getting the Compass Heading 89
3.11. Accessing the User’s Location 90
3.12. Calculating the User’s Speed 93
3.13. Pinpointing the User’s Proximity to Landmarks 94
3.14. Receiving Notifications When the User Changes Location 95
3.15. Looking Up GPS Coordinates for a Street Address 99
3.16. Looking Up Street Addresses from the User’s Location 100
3.17. Using the Device as a Steering Wheel 101
3.18. Detecting Magnets 102
3.19. Utilizing Inputs to Improve Game Design 104
4. Sound. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.1. Playing Sound with AVAudioPlayer 105
4.2. Recording Sound with AVAudioRecorder 108
4.3. Working with Multiple Audio Players 110
4.4. Cross-Fading Between Tracks 112
4.5. Synthesizing Speech 114
4.6. Getting Information About What the iPod Is Playing 115
4.7. Detecting When the Currently Playing Track Changes 117
4.8. Controlling iPod Playback 118
4.9. Allowing the User to Select Music 119
iv | Table of Contents
www.it-ebooks.info
4.10. Cooperating with Other Applications’ Audio 122
4.11. Determining How to Best Use Sound in Your Game Design 123
5. Data Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
5.1. Saving the State of Your Game 125
5.2. Storing High Scores Locally 128
5.3. Using iCloud to Save Games 129
5.4. Using the iCloud Key/Value Store 132
5.5. Loading Structured Information 134
5.6. Deciding When to Use Files or a Database 136
5.7. Using SQLite for Storage 137
5.8. Managing a Collection of Assets 139
5.9. Storing Information in NSUserDefaults 142
5.10. Implementing the Best Data Storage Strategy 144
5.11. In-Game Currency 144
6. 2D Graphics and Sprite Kit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
6.1. Getting Familiar with 2D Math 147
6.2. Creating a Sprite Kit View 152
6.3. Creating a Scene 154
6.4. Adding a Sprite 156
6.5. Adding a Text Sprite 157
6.6. Determining Available Fonts 159
6.7. Including Custom Fonts 160
6.8. Transitioning Between Scenes 160
6.9. Moving Sprites and Labels Around 162
6.10. Adding a Texture Sprite 165
6.11. Creating Texture Atlases 165
6.12. Using Shape Nodes 166
6.13. Using Blending Modes 167
6.14. Using Image Effects to Change the Way that Sprites Are Drawn 169
6.15. Using Bézier Paths 170
6.16. Creating Smoke, Fire, and Other Particle Effects 171
6.17. Shaking the Screen 172
6.18. Animating a Sprite 174
6.19. Parallax Scrolling 175
6.20. Creating Images Using Perlin Noise 182
7. Physics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
7.1. Reviewing Physics Terms and Definitions 191
7.2. Adding Physics to Sprites 193
7.3. Creating Static and Dynamic Objects 194
Table of Contents | v
www.it-ebooks.info
7.4. Defining Collider Shapes 195
7.5. Setting Velocities 197
7.6. Working with Mass, Size, and Density 198
7.7. Creating Walls in Your Scene 199
7.8. Controlling Gravity 201
7.9. Keeping Objects from Falling Over 202
7.10. Controlling Time in Your Physics Simulation 202
7.11. Detecting Collisions 203
7.12. Finding Objects 204
7.13. Working with Joints 206
7.14. Working with Forces 207
7.15. Adding Thrusters to Objects 208
7.16. Creating Explosions 209
7.17. Using Device Orientation to Control Gravity 211
7.18. Dragging Objects Around 212
7.19. Creating a Car 215
8. 3D Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
8.1. Working with 3D Math 219
8.2. Creating a GLKit Context 223
8.3. Drawing a Square Using OpenGL 225
8.4. Loading a Texture 233
8.5. Drawing a Cube 235
8.6. Rotating a Cube 238
8.7. Moving the Camera in 3D Space 239
9. Intermediate 3D Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
9.1. Loading a Mesh 241
9.2. Parenting Objects 248
9.3. Animating a Mesh 252
9.4. Batching Draw Calls 255
9.5. Creating a Movable Camera Object 256
10. Advanced 3D Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
10.1. Understanding Shaders 261
10.2. Working with Materials 265
10.3. Texturing with Shaders 271
10.4. Lighting a Scene 272
10.5. Using Normal Mapping 275
10.6. Making Objects Transparent 277
10.7. Adding Specular Highlights 278
vi | Table of Contents
www.it-ebooks.info
10.8. Adding Toon Shading 280
11. Artificial Intelligence and Behavior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
11.1. Making an Object Move Toward a Position 283
11.2. Making Things Follow a Path 285
11.3. Making an Object Intercept a Moving Target 286
11.4. Making an Object Flee When It’s in Trouble 287
11.5. Making an Object Decide on a Target 288
11.6. Making an Object Steer Toward a Point 289
11.7. Making an Object Know Where to Take Cover 290
11.8. Calculating a Path for an Object to Take 291
11.9. Finding the Next Best Move for a Puzzle Game 296
11.10. Determining if an Object Can See Another Object 297
11.11. Using AI to Enhance Your Game Design 299
12. Networking and Social Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
12.1. Using Game Center 301
12.2. Getting Information About the Logged-in Player 305
12.3. Getting Information About Other Players 305
12.4. Making Leaderboards and Challenges with Game Center 306
12.5. Finding People to Play with Using Game Center 310
12.6. Using Bluetooth to Detect Nearby Game Players with the Multipeer
Connectivity Framework 312
12.7. Making Real-Time Gameplay Work with Game Kit and the Multipeer
Connectivity Framework 315
12.8. Creating, Destroying, and Synchronizing Objects on the Network 317
12.9. Interpolating Object State 318
12.10. Handling When a Player Disconnects and Rejoins 320
12.11. Making Turn-Based Gameplay Work with GameKit 321
12.12. Sharing Text and Images to Social Media Sites 324
12.13. Implementing iOS Networking Effectively 325
12.14. Implementing Social Networks Effectively 326
13. Game Controllers and External Screens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
13.1. Detecting Controllers 329
13.2. Getting Input from a Game Controller 331
13.3. Showing Content via AirPlay 332
13.4. Using External Screens 333
13.5. Designing Effective Graphics for Different Screens 335
13.6. Dragging and Dropping 338
14. Performance and Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Table of Contents | vii
www.it-ebooks.info
14.1. Improving Your Frame Rate 345
14.2. Making Levels Load Quickly 348
14.3. Dealing with Low-Memory Issues 349
14.4. Tracking Down a Crash 351
14.5. Working with Compressed Textures 352
14.6. Working with Watchpoints 355
14.7. Logging Effectively 356
14.8. Creating Breakpoints That Use Speech 358
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
viii | Table of Contents
www.it-ebooks.info