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 9
1.6 Updating Based on a Timer 11
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 14
1.10 Working with Closures 15
1.11 Writing a Method That Calls a Closure 17
1.12 Working with Operation Queues 18
1.13 Performing a Task in the Future 20
1.14 Making Operations Depend on Each Other 21
1.15 Filtering an Array with Closures 22
1.16 Loading New Assets During Gameplay 22
1.17 Adding Unit Tests to Your Game 24
1.18 2D Grids 26
2 Views and Menus 31
2.1 Working with Storyboards 32
2.2 Creating View Controllers 38
2.3 Using Segues to Move Between Screens 45
2.4 Using Constraints to Lay Out Views 49
2.5 Adding Images to Your Project 51
2.6 Slicing Images for Use in Buttons 53
2.7 Using UI Dynamics to Make Animated Views 55
2.8 Moving an Image with Core Animation 56
2.9 Rotating an Image 58
2.10 Animating a Popping Effect on a View 60
2.11 Theming UI Elements with UIAppearance 62
2.12 Rotating a UTView in 3D 63
2.13 Overlaying Menus on Top of Game Content 65
2.14 Designing Effective Game Menus 66
3 Input 67
3.1 Detecting When a View Is Touched 68
3.2 Responding to Tap Gestures 69
3.3 Dragging an Image Around the Screen 70
3.4 Detecting Rotation Gestures 72
3.5 Detecting Pinching Gestures 75
3.6 Creating Custom Gestures 76
3.7 Receiving Touches in Custom Areas of a View 80
3.8 Detecting Shakes 81
3.9 Detecting Device Tilt 83
3.10 Getting the Compass Heading 85
3.11 Accessing the User's Location 87
3.12 Calculating the Users Speed 90
3.13 Pinpointing the User's Proximity to Landmarks 91
3.14 Receiving Notifications When the User Changes Location 92
3.15 Looking Up GPS Coordinates for a Street Address 95
3.16 Looking Up Street Addresses from the Users Location 97
3.17 Using the Device as a Steering Wheel 98
3.18 Detecting Magnets 99
3.19 Utilizing Inputs to Improve Game Design 101
4 Sound 103
4.1 Playing Sound with AVAudioPlayer 103
4.2 Recording Sound with AVAudioRecorder 106
4.3 Working with Multiple Audio Players 108
4.4 Cross-Fading Between Tracks 109
4.5 Synthesizing Speech 111
4.6 Getting Information About What the Music App Is Playing 113
4.7 Detecting When the Currently Playing Track Changes 114
4.8 Controlling Music Playback 116
4.9 Allowing the User to Select Music 117
4.10 Cooperating with Other Applications' Audio 119
4.11 Determining How to Best Use Sound in Your Game Design 121
5 Data Storage 123
5.1 Saving the State of Your Game 123
5.2 Storing High Scores Locally 126
5.3 Using iCloud to Save Games 128
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 Managing a Collection of Assets 137
5.8 Storing Information in NSUserDefaults 139
5.9 Implementing the Best Data Storage Strategy 141
5.10 In-Game Currency 141
6 2D Graphics and Sprite Kit 143
6.1 Getting Familiar with 2D Math 143
6.2 Creating a Sprite Kit View 149
6.3 Creating a Scene 150
6.4 Adding a Sprite 152
6.5 Adding a Text Sprite 153
6.6 Determining Available Fonts 155
6.7 Including Custom Fonts 156
6.8 Transitioning Between Scenes 156
6.9 Moving Sprites and Labels Around 158
6.10 Adding a Texture Sprite 161
6.11 Creating Texture Atlases 162
6.12 Using Shape Nodes 162
6.13 Using Blending Modes 163
6.14 Using Image Effects to Change the Way That Sprites Are Drawn 165
6.15 Using Bézier Paths 166
6.16 Creating Smoke, Fire, and Other Particle Effects 167
6.17 Shaking the Screen 168
6.18 Animating a Sprite 170
6.19 Parallax Scrolling 171
6.20 Creating Images Using Noise 178
7 Physics 181
7.1 Reviewing Physics Terms and Definitions 181
7.2 Adding Physics to Sprites 183
7.3 Creating Static and Dynamic Objects 184
7.4 Defining Collider Shapes 185
7.5 Setting Velocities 187
7.6 Working with Mass, Size, and Density 188
7.7 Creating Walls in Your Scene 189
7.8 Controlling Gravity 191
7.9 Keeping Objects from Falling Over 192
7.10 Controlling Time in Your Physics Simulation 192
7.11 Detecting Collisions 193
7.12 Finding Objects 194
7.13 Working with Joints 196
7.14 Working with Forces 197
7.15 Adding Thrusters to Objects 198
7.16 Creating Explosions 199
7.17 Using Device Orientation to Control Gravity 201
7.18 Dragging Objects Around 202
7.19 Creating a Car 205
8 3D Graphics 209
8.1 Working with 3D Math 210
8.2 Creating a GLKit Context 214
8.3 Drawing a Square Using OpenGL 216
8.4 Loading a Texture 224
8.5 Drawing a Cube 227
8.6 Rotating a Cube 231
8.7 Moving the Camera in 3D Space 232
9 Intermediate 3D Graphics 235
9.1 Loading a Mesh 235
9.2 Parenting Objects 242
9.3 Animating a Mesh 246
9.4 Batching Draw Calls 249
9.5 Creating a Movable Camera Object 250
10 Advanced 3D Graphics 255
10.1 Understanding Shaders 255
10.2 Working with Materials 259
10.3 Texturing with Shaders 265
10.4 Lighting a Scene 266
10.5 Using Normal Mapping 269
10.6 Making Objects Transparent 271
10.7 Adding Specular Highlights 273
10.8 Adding Toon Shading 276
11 Scence Kit 279
11.1 Setting Up for Scene Kit 279
11.2 Creating a Scene Kit Scene 280
11.3 Showing a 3D Object 280
11.4 Working with Scene Kit Cameras 281
11.5 Creating Lights 282
11.6 Animating Objects 283
11.7 Working with Text Nodes 284
11.8 Customizing Materials 285
11.9 Texturing Objects 286
11.10 Normal Mapping 286
11.11 Constraining Objects 287
11.12 Loading COLLADA Files 288
11.13 Using 3D Physics 289
11.14 Adding Reflections 290
11.15 Hit-Testing the Scene 290
12 Artificial Intelligence and Behavior 293
12.1 Making Vector Math Nicer in Swift 293
12.2 Making an Object Move Toward a Position 295
12.3 Making Things Follow a Path 297
12.4 Making an Object Intercept a Moving Target 298
12.5 Making an Object Flee When It's in Trouble 299
12.6 Making an Object Decide on a Target 300
12.7 Making an Object Steer Toward a Point 301
12.8 Making an Object Know Where to Take Cover 302
12.9 Calculating a Path for an Object to Take 303
12.10 Finding the Next Best Move for a Puzzle Game 308
12.11 Determining If an Object Can See Another Object 309
12.12 Using AI to Enhance Your Game Design 311
13 Networking and Social Media 313
13.1 Using Game Center 313
13.2 Getting Information About the Logged-in Player 320
13.3 Getting Information About Other Players 320
13.4 Making Leaderboards and Challenges with Game Center 321
13.5 Finding People to Play with Using Game Center 325
13.6 Creating, Destroying, and Synchronizing Objects on the Network 327
13.7 Interpolating Object State 329
13.8 Handling When a Player Disconnects and Rejoins 331
13.9 Making Turn-Based Gameplay Work with Game Kit 332
13.10 Sharing Text and Images to Social Media Sites 335
13.11 Storing Saved Games in Game Center 336
13.12 Implementing iOS Networking Effectively 338
13.13 Implementing Social Networks Effectively 338
14 Game Controllers and External Screens 341
14.1 Detecting Controllers 343
14.2 Getting Input from a Game Controller 345
14.3 Showing Content via AirPlay 347
14.4 Using External Screens 348
14.5 Designing Effective Graphics for Different Screens 350
14.6 Dragging and Dropping 352
15 Performance and Debugging 359
15.1 Improving Your Frame Rate 359
15.2 Making Levels Load Quickly 361
15.3 Dealing with Low-Memory Issues 363
15.4 Tracking Down a Crash 365
15.5 Working with Compressed Textures 366
15.6 Working with Watchpoints 370
15.7 Logging Effectively 371
15.8 Creating Breakpoints That Use Speech 372
Index 375