You are previewing iOS Swift Game Development Cookbook, 2nd Edition.
O'Reilly logo
iOS Swift Game Development Cookbook, 2nd Edition

Book Description

Ready to make amazing games for the iPhone, iPad, and iPod touch? With Apple’s Swift programming language, it’s never been easier. This updated cookbook provides detailed recipes for a managing wide range of common iOS game-development issues, ranging from 2D and 3D math to SpriteKit and OpenGL to performance—all revised for Swift.

You get simple, direct solutions to common problems found in iOS game programming. Need to figure out how to give objects physical motion, or want a refresher on gaming-related math problems? This book provides sample projects and straightforward answers. All you need to get started is some familiarity with iOS development, Swift, and Objective-C.

Table of Contents

  1. Preface
    1. Audience
    2. Organization of This Book
    3. Additional Resources
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  2. 1. Laying Out a Game
    1. Laying Out Your Engine
      1. Problem
      2. Solution
      3. Discussion
    2. Creating an Inheritance-Based Game Layout
      1. Problem
      2. Solution
      3. Discussion
    3. Creating a Component-Based Game Layout
      1. Problem
      2. Solution
      3. Discussion
    4. Calculating Delta Times
      1. Problem
      2. Solution
      3. Discussion
    5. Detecting When the User Enters and Exits Your Game
      1. Problem
      2. Solution
      3. Discussion
    6. Updating Based on a Timer
      1. Problem
      2. Solution
      3. Discussion
    7. Updating Based on When the Screen Updates
      1. Problem
      2. Solution
      3. Discussion
    8. Pausing a Game
      1. Problem
      2. Solution
      3. Discussion
    9. Calculating Time Elapsed Since the Game Start
      1. Problem
      2. Solution
      3. Discussion
    10. Working with Closures
      1. Problem
      2. Solution
      3. Discussion
        1. How closures work
        2. Closures and other objects
    11. Writing a Method That Calls a Closure
      1. Problem
      2. Solution
      3. Discussion
    12. Working with Operation Queues
      1. Problem
      2. Solution
      3. Discussion
    13. Performing a Task in the Future
      1. Problem
      2. Solution
      3. Discussion
    14. Making Operations Depend on Each Other
      1. Problem
      2. Solution
      3. Discussion
    15. Filtering an Array with Closures
      1. Problem
      2. Solution
      3. Discussion
    16. Loading New Assets During Gameplay
      1. Problem
      2. Solution
      3. Discussion
    17. Adding Unit Tests to Your Game
      1. Problem
      2. Solution
      3. Discussion
    18. 2D Grids
      1. Problem
      2. Solution
      3. Discussion
  3. 2. Views and Menus
    1. Working with Storyboards
      1. Problem
      2. Solution
    2. Creating View Controllers
      1. Problem
      2. Solution
      3. Discussion
    3. Using Segues to Move Between Screens
      1. Problem
      2. Solution
      3. Discussion
    4. Using Constraints to Lay Out Views
      1. Problem
      2. Solution
      3. Discussion
    5. Adding Images to Your Project
      1. Problem
      2. Solution
      3. Discussion
    6. Slicing Images for Use in Buttons
      1. Problem
      2. Solution
      3. Discussion
    7. Using UI Dynamics to Make Animated Views
      1. Problem
      2. Solution
        1. Adding gravity to views
        2. Adding collision
        3. Adding attachment
      3. Discussion
    8. Moving an Image with Core Animation
      1. Problem
      2. Solution
      3. Discussion
    9. Rotating an Image
      1. Problem
      2. Solution
      3. Discussion
    10. Animating a Popping Effect on a View
      1. Problem
      2. Solution
      3. Discussion
    11. Theming UI Elements with UIAppearance
      1. Problem
      2. Solution
      3. Discussion
    12. Rotating a UIView in 3D
      1. Problem
      2. Solution
      3. Discussion
    13. Overlaying Menus on Top of Game Content
      1. Problem
      2. Solution
      3. Discussion
    14. Designing Effective Game Menus
      1. Problem
      2. Solution
      3. Discussion
  4. 3. Input
    1. Detecting When a View Is Touched
      1. Problem
      2. Solution
      3. Discussion
    2. Responding to Tap Gestures
      1. Problem
      2. Solution
      3. Discussion
    3. Dragging an Image Around the Screen
      1. Problem
      2. Solution
      3. Discussion
    4. Detecting Rotation Gestures
      1. Problem
      2. Solution
      3. Discussion
    5. Detecting Pinching Gestures
      1. Problem
      2. Solution
      3. Discussion
    6. Creating Custom Gestures
      1. Problem
      2. Solution
      3. Discussion
    7. Receiving Touches in Custom Areas of a View
      1. Problem
      2. Solution
      3. Discussion
    8. Detecting Shakes
      1. Problem
      2. Solution
      3. Discussion
    9. Detecting Device Tilt
      1. Problem
      2. Solution
      3. Discussion
    10. Getting the Compass Heading
      1. Problem
      2. Solution
      3. Discussion
    11. Accessing the User’s Location
      1. Problem
      2. Solution
      3. Discussion
    12. Calculating the User’s Speed
      1. Problem
      2. Solution
      3. Discussion
    13. Pinpointing the User’s Proximity to Landmarks
      1. Problem
      2. Solution
      3. Discussion
    14. Receiving Notifications When the User Changes Location
      1. Problem
      2. Solution
      3. Discussion
    15. Looking Up GPS Coordinates for a Street Address
      1. Problem
      2. Solution
      3. Discussion
    16. Looking Up Street Addresses from the User’s Location
      1. Problem
      2. Solution
      3. Discussion
    17. Using the Device as a Steering Wheel
      1. Problem
      2. Solution
      3. Discussion
    18. Detecting Magnets
      1. Problem
      2. Solution
      3. Discussion
    19. Utilizing Inputs to Improve Game Design
      1. Problem
      2. Solution
      3. Discussion
  5. 4. Sound
    1. Playing Sound with AVAudioPlayer
      1. Problem
      2. Solution
      3. Discussion
    2. Recording Sound with AVAudioRecorder
      1. Problem
      2. Solution
      3. Discussion
    3. Working with Multiple Audio Players
      1. Problem
      2. Solution
      3. Discussion
    4. Cross-Fading Between Tracks
      1. Problem
      2. Solution
      3. Discussion
    5. Synthesizing Speech
      1. Problem
      2. Solution
      3. Discussion
    6. Getting Information About What the Music App Is Playing
      1. Problem
      2. Solution
      3. Discussion
    7. Detecting When the Currently Playing Track Changes
      1. Problem
      2. Solution
      3. Discussion
    8. Controlling Music Playback
      1. Problem
      2. Solution
      3. Discussion
    9. Allowing the User to Select Music
      1. Problem
      2. Solution
      3. Discussion
    10. Cooperating with Other Applications’ Audio
      1. Problem
      2. Solution
      3. Discussion
    11. Determining How to Best Use Sound in Your Game Design
      1. Problem
      2. Solution
      3. Discussion
  6. 5. Data Storage
    1. Saving the State of Your Game
      1. Problem
      2. Solution
      3. Discussion
    2. Storing High Scores Locally
      1. Problem
      2. Solution
      3. Discussion
    3. Using iCloud to Save Games
      1. Problem
      2. Solution
      3. Discussion
    4. Using the iCloud Key-Value Store
      1. Problem
      2. Solution
      3. Discussion
    5. Loading Structured Information
      1. Problem
      2. Solution
      3. Discussion
    6. Deciding When to Use Files or a Database
      1. Problem
      2. Solution
      3. Discussion
    7. Managing a Collection of Assets
      1. Problem
      2. Solution
      3. Discussion
    8. Storing Information in NSUserDefaults
      1. Problem
      2. Solution
      3. Discussion
    9. Implementing the Best Data Storage Strategy
      1. Problem
      2. Solution
      3. Discussion
    10. In-Game Currency
      1. Problem
      2. Solution
      3. Discussion
  7. 6. 2D Graphics and Sprite Kit
    1. Getting Familiar with 2D Math
      1. Coordinate System
      2. Vectors
        1. Vector lengths
        2. Moving vectors
        3. Rotating vectors
        4. Scaling vectors
        5. Dot product
    2. Creating a Sprite Kit View
      1. Problem
      2. Solution
      3. Discussion
    3. Creating a Scene
      1. Problem
      2. Solution
      3. Discussion
    4. Adding a Sprite
      1. Problem
      2. Solution
      3. Discussion
    5. Adding a Text Sprite
      1. Problem
      2. Solution
      3. Discussion
    6. Determining Available Fonts
      1. Problem
      2. Solution
      3. Discussion
    7. Including Custom Fonts
      1. Problem
      2. Solution
      3. Discussion
    8. Transitioning Between Scenes
      1. Problem
      2. Solution
      3. Discussion
    9. Moving Sprites and Labels Around
      1. Problem
      2. Solution
      3. Discussion
    10. Adding a Texture Sprite
      1. Problem
      2. Solution
      3. Discussion
    11. Creating Texture Atlases
      1. Problem
      2. Solution
      3. Discussion
    12. Using Shape Nodes
      1. Problem
      2. Solution
      3. Discussion
    13. Using Blending Modes
      1. Problem
      2. Solution
      3. Discussion
    14. Using Image Effects to Change the Way That Sprites Are Drawn
      1. Problem
      2. Solution
      3. Discussion
    15. Using Bézier Paths
      1. Problem
      2. Solution
      3. Discussion
    16. Creating Smoke, Fire, and Other Particle Effects
      1. Problem
      2. Solution
      3. Discussion
    17. Shaking the Screen
      1. Problem
      2. Solution
      3. Discussion
    18. Animating a Sprite
      1. Problem
      2. Solution
      3. Discussion
    19. Parallax Scrolling
      1. Problem
      2. Solution
      3. Discussion
    20. Creating Images Using Noise
      1. Problem
      2. Solution
      3. Discussion
  8. 7. Physics
    1. Reviewing Physics Terms and Definitions
    2. Adding Physics to Sprites
      1. Problem
      2. Solution
      3. Discussion
    3. Creating Static and Dynamic Objects
      1. Problem
      2. Solution
      3. Discussion
    4. Defining Collider Shapes
      1. Problem
      2. Solution
      3. Discussion
    5. Setting Velocities
      1. Problem
      2. Solution
      3. Discussion
    6. Working with Mass, Size, and Density
      1. Problem
      2. Solution
      3. Discussion
    7. Creating Walls in Your Scene
      1. Problem
      2. Solution
      3. Discussion
    8. Controlling Gravity
      1. Problem
      2. Solution
      3. Discussion
    9. Keeping Objects from Falling Over
      1. Problem
      2. Solution
      3. Discussion
    10. Controlling Time in Your Physics Simulation
      1. Problem
      2. Solution
      3. Discussion
    11. Detecting Collisions
      1. Problem
      2. Solution
      3. Discussion
    12. Finding Objects
      1. Problem
      2. Solution
      3. Discussion
    13. Working with Joints
      1. Problem
      2. Solution
      3. Discussion
    14. Working with Forces
      1. Problem
      2. Solution
      3. Discussion
    15. Adding Thrusters to Objects
      1. Problem
      2. Solution
      3. Discussion
    16. Creating Explosions
      1. Problem
      2. Solution
      3. Discussion
    17. Using Device Orientation to Control Gravity
      1. Problem
      2. Solution
      3. Discussion
    18. Dragging Objects Around
      1. Problem
      2. Solution
      3. Discussion
    19. Creating a Car
      1. Problem
      2. Solution
      3. Discussion
  9. 8. 3D Graphics
    1. Working with 3D Math
      1. 3D Vectors and GLKit
      2. Matrices
    2. Creating a GLKit Context
      1. Problem
      2. Solution
      3. Discussion
    3. Drawing a Square Using OpenGL
      1. Problem
      2. Solution
      3. Discussion
    4. Loading a Texture
      1. Problem
      2. Solution
      3. Discussion
    5. Drawing a Cube
      1. Problem
      2. Solution
      3. Discussion
    6. Rotating a Cube
      1. Problem
      2. Solution
      3. Discussion
    7. Moving the Camera in 3D Space
      1. Problem
      2. Solution
      3. Discussion
  10. 9. Intermediate 3D Graphics
    1. Loading a Mesh
      1. Problem
      2. Solution
      3. Discussion
    2. Parenting Objects
      1. Problem
      2. Solution
      3. Discussion
    3. Animating a Mesh
      1. Problem
      2. Solution
      3. Discussion
    4. Batching Draw Calls
      1. Problem
      2. Solution
      3. Discussion
    5. Creating a Movable Camera Object
      1. Problem
      2. Solution
      3. Discussion
  11. 10. Advanced 3D Graphics
    1. Understanding Shaders
      1. Problem
      2. Solution
      3. Discussion
    2. Working with Materials
      1. Problem
      2. Solution
      3. Discussion
    3. Texturing with Shaders
      1. Problem
      2. Solution
      3. Discussion
    4. Lighting a Scene
      1. Problem
      2. Solution
      3. Discussion
    5. Using Normal Mapping
      1. Problem
      2. Solution
      3. Discussion
    6. Making Objects Transparent
      1. Problem
      2. Solution
      3. Discussion
    7. Adding Specular Highlights
      1. Problem
      2. Solution
      3. Discussion
    8. Adding Toon Shading
      1. Problem
      2. Solution
      3. Discussion
  12. 11. Scene Kit
    1. Setting Up for Scene Kit
      1. Problem
      2. Solution
      3. Discussion
    2. Creating a Scene Kit Scene
      1. Problem
      2. Solution
      3. Discussion
    3. Showing a 3D Object
      1. Problem
      2. Solution
      3. Discussion
    4. Working with Scene Kit Cameras
      1. Problem
      2. Solution
      3. Discussion
    5. Creating Lights
      1. Problem
      2. Solution
      3. Discussion
    6. Animating Objects
      1. Problem
      2. Solution
      3. Discussion
    7. Working with Text Nodes
      1. Problem
      2. Solution
      3. Discussion
    8. Customizing Materials
      1. Problem
      2. Solution
      3. Discussion
    9. Texturing Objects
      1. Problem
      2. Solution
      3. Discussion
    10. Normal Mapping
      1. Problem
      2. Solution
      3. Discussion
    11. Constraining Objects
      1. Problem
      2. Solution
      3. Discussion
    12. Loading COLLADA Files
      1. Problem
      2. Solution
      3. Discussion
    13. Using 3D Physics
      1. Problem
      2. Solution
      3. Discussion
    14. Adding Reflections
      1. Problem
      2. Solution
      3. Discussion
    15. Hit-Testing the Scene
      1. Problem
      2. Solution
      3. Discussion
  13. 12. Artificial Intelligence and Behavior
    1. Making Vector Math Nicer in Swift
      1. Problem
      2. Solution
      3. Discussion
    2. Making an Object Move Toward a Position
      1. Problem
      2. Solution
      3. Discussion
    3. Making Things Follow a Path
      1. Problem
      2. Solution
      3. Discussion
    4. Making an Object Intercept a Moving Target
      1. Problem
      2. Solution
      3. Discussion
    5. Making an Object Flee When It’s in Trouble
      1. Problem
      2. Solution
      3. Discussion
    6. Making an Object Decide on a Target
      1. Problem
      2. Solution
      3. Discussion
    7. Making an Object Steer Toward a Point
      1. Problem
      2. Solution
      3. Discussion
    8. Making an Object Know Where to Take Cover
      1. Problem
      2. Solution
      3. Discussion
    9. Calculating a Path for an Object to Take
      1. Problem
      2. Solution
      3. Discussion
    10. Finding the Next Best Move for a Puzzle Game
      1. Problem
      2. Solution
      3. Discussion
    11. Determining If an Object Can See Another Object
      1. Problem
      2. Solution
      3. Discussion
    12. Using AI to Enhance Your Game Design
      1. Problem
      2. Solution
      3. Discussion
  14. 13. Networking and Social Media
    1. Using Game Center
    2. Getting Information About the Logged-in Player
      1. Problem
      2. Solution
      3. Discussion
    3. Getting Information About Other Players
      1. Problem
      2. Solution
      3. Discussion
    4. Making Leaderboards and Challenges with Game Center
      1. Problem
      2. Solution
      3. Discussion
    5. Finding People to Play with Using Game Center
      1. Problem
      2. Solution
      3. Discussion
    6. Creating, Destroying, and Synchronizing Objects on the Network
      1. Problem
      2. Solution
      3. Discussion
    7. Interpolating Object State
      1. Problem
      2. Solution
      3. Discussion
    8. Handling When a Player Disconnects and Rejoins
      1. Problem
      2. Solution
      3. Discussion
    9. Making Turn-Based Gameplay Work with Game Kit
      1. Problem
      2. Solution
      3. Discussion
    10. Sharing Text and Images to Social Media Sites
      1. Problem
      2. Solution
      3. Discussion
    11. Storing Saved Games in Game Center
      1. Problem
      2. Solution
      3. Discussion
    12. Implementing iOS Networking Effectively
      1. Problem
      2. Solution
      3. Discussion
    13. Implementing Social Networks Effectively
      1. Problem
      2. Solution
      3. Discussion
  15. 14. Game Controllers and External Screens
    1. Detecting Controllers
      1. Problem
      2. Solution
      3. Discussion
    2. Getting Input from a Game Controller
      1. Problem
      2. Solution
      3. Discussion
    3. Showing Content via AirPlay
      1. Problem
      2. Solution
      3. Discussion
    4. Using External Screens
      1. Problem
      2. Solution
      3. Discussion
    5. Designing Effective Graphics for Different Screens
      1. Problem
      2. Solution
        1. Designing for iPhones
        2. Designing for iPads
        3. Designing for larger screens
      3. Discussion
    6. Dragging and Dropping
      1. Problem
      2. Solution
      3. Discussion
  16. 15. Performance and Debugging
    1. Improving Your Frame Rate
      1. Problem
      2. Solution
      3. Discussion
    2. Making Levels Load Quickly
      1. Problem
      2. Solution
      3. Discussion
    3. Dealing with Low-Memory Issues
      1. Problem
      2. Solution
      3. Discussion
    4. Tracking Down a Crash
      1. Problem
      2. Solution
      3. Discussion
        1. Fixing exceptions
        2. Fixing memory pressure issues
    5. Working with Compressed Textures
      1. Problem
      2. Solution
      3. Discussion
    6. Working with Watchpoints
      1. Problem
      2. Solution
      3. Discussion
    7. Logging Effectively
      1. Problem
      2. Solution
      3. Discussion
    8. Creating Breakpoints That Use Speech
      1. Problem
      2. Solution
      3. Discussion
  17. Index
  18. Colophon
  19. Copyright