Preface

Games rule mobile devices. The iPhone, iPad, and iPod touch are all phenomenally powerful gaming platforms, and making amazing games that your players can access at a moment’s notice has never been easier. The iTunes App Store category with the most apps is Games; it includes games ranging from simple one-minute puzzle games to in-depth, long-form adventures. The time to jump in and make your own games has never been better. We say this having been in iOS game development since the App Store opened. Our first iOS game, in 2008, a little strategy puzzler named Culture, led us to working on hundreds of other awesome projects, ranging from a digital board game for museums to educational children’s games, and everything in between! The possibilities for games on this platform are only becoming wider and wider.

This book provides you with simple, direct solutions to common problems found in iOS game programming using Swift. Whether you’re stuck figuring out how to give objects physical motion, or just want a refresher on common gaming-related math problems, you’ll find simple, straightforward answers, explanations, and sample projects. This book is part tutorial and part reference. It’s something you’ll want to keep handy to get new ideas about what’s possible through a series of recipes, as well as for a quick guide to find answers on a number of topics.

Audience

We assume that you’re a reasonably capable programmer, and that you know at least a little bit about developing for iOS: what Xcode is and how to get around in it, how to use the iOS Simulator, and the basics of the Swift programming language. We also assume you know how to use an iOS device. We don’t assume any existing knowledge of game development on any platform, but we’re guessing that you’re reading this book with a vague idea about the kind of game you’d like to make.

This book isn’t based on any particular genre of games—you’ll find the recipes in it applicable to all kinds of games, though some will suit some genres more than others.

Organization of This Book

Each chapter of this book contains recipes: short solutions to common problems found in game development. The book is designed to be read in any order; you don’t need to read it cover-to-cover, and you don’t need to read any chapter from start to finish. (Of course, we encourage doing that, because you’ll probably pick up on stuff you didn’t realize you wanted to know.)

Each recipe is structured like this: the problem being addressed is presented, followed by the solution, which explains the technique of solving the problem (or implementing the feature, and so on). Following the solution, the recipe contains a discussion that goes into more detail on the solution, which gives you more information about what the solution does, other things to watch out for, and other useful knowledge.

Here is a concise breakdown of the material each chapter covers:

Chapter 1, Laying Out a Game

This chapter discusses different ways to design the architecture and code layout of your game, how to work with timers in a variety of ways, and how blocks work in iOS. You’ll also learn how to schedule work to be performed in the future using blocks and operation queues, and how to add unit tests to your project.

Chapter 2, Views and Menus

This chapter focuses on interface design and working with UIKit, the built-in system for displaying user interface graphics. In addition to providing common objects like buttons and text fields, UIKit can be customized to suit your needs—for some kinds of games, UIKit might be the only graphical tool you’ll need.

Chapter 3, Input

In this chapter, you’ll learn how to get input from the user so that you can apply it to your game. This includes touching the screen, detecting different types of gestures (such as tapping, swiping, and pinching), as well as other kinds of input like the user’s current position on the planet, or motion information from the variety of built-in sensors in the device.

Chapter 4, Sound

This chapter discusses how to work with sound effects and music. You’ll learn how to load and play audio files, how to work with the user’s built-in music library, and how to make your game’s sound work well when the user wants to listen to his or her music while playing your game.

Chapter 5, Data Storage

This chapter is all about storing information for later use. The information that games need to save ranges from the very small (such as high scores), to medium (saved games), all the way up to very large (collections of game assets). In this chapter, you’ll learn about the many different ways that information can be stored and accessed, and which of these is best suited for what you want to do, as well as how to make use of iCloud features.

Chapter 6, 2D Graphics and SpriteKit

This chapter discusses SpriteKit, the 2D graphics system built into iOS. SpriteKit is both powerful and very easy to use; in this chapter, you’ll learn how to create a scene, how to animate sprites, and how to work with textures and images. This chapter also provides you with info you can use to brush up on your 2D math skills.

Chapter 7, Physics

In this chapter, you’ll learn how to use the 2D physics simulation that’s provided as part of SpriteKit. Physics simulation is a great way to make your game’s movements feel more realistic, and you can use it to get a lot of great-feeling game for very little programmer effort. You’ll learn how to work with physics bodies, joints, and forces, as well as how to take user input and make it control your game’s physical simulation.

Chapter 8, SceneKit

This chapter covers Scene Kit, Apple’s new 3D framework. It has recipes for showing 3D objects with Scene Kit; working with cameras, lights, and textures; and using physics in 3D.

Chapter 9, Artificial Intelligence and Behavior

This chapter discusses how to make objects in your game behave on their own, and react to the player. You’ll learn how to make one object chase another, how to make objects flee from something, and how to work out a path from one point to another while avoiding obstacles. You’ll also learn how to use Core ML and Vision to detect objects seen by the camera.

Chapter 10, Working with the Outside World

This chapter discusses the ways that your game can interact with the outside world: external displays, like televisions and monitors; game controllers that provide additional input methods, like thumbsticks and physical buttons; and cameras. You’ll learn how to detect, use, and design your game to take advantage of additional hardware where it’s present. You’ll also learn how to make use of ARKit for augmented reality.

Chapter 11, Performance and Debugging

The last chapter of the book looks at improving your game’s performance and stability. You’ll learn how to take advantage of advanced Xcode debugging features, how to use compressed textures to save memory, and how to make your game load faster.

Additional Resources

You can download the code samples from this book (or fork using GitHub) at http://www.secretlab.com.au/books/ios-game-dev-cookbook-swift.

O’Reilly has a number of other excellent books on game development and software development (both generally and related to iOS) that can help you on your iOS game development journey, including:

We strongly recommend that you add Gamasutra to your regular reading list, due to its high-quality coverage of game industry news.

Game designer Marc LeBlanc’s website is where he collects various presentations, notes, and essays. We’ve found him to be a tremendous inspiration.

Finally, we’d be remiss if we didn’t link to our own blog.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at http://www.secretlab.com.au/books/ios-game-dev-cookbook-swift.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “iOS Swift Game Development Cookbook, Third Edition, by Jonathan Manning and Paris Buttfield-Addison (O’Reilly). Copyright 2018 Secret Lab, 978-1-491-99908-0.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/ios-swift-game-dev-errata.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Jon thanks his mother, father, and the rest of his crazily extended family for their tremendous support.

Paris thanks his mother, without whom he wouldn’t be doing anything nearly as interesting, let alone writing books.

We’d all like to thank Rachel Roumeliotis, whose skill and advice were invaluable to completing the book. Likewise, all the O’Reilly Media staff we’ve interacted with over the course of writing the book have been the absolute gurus of their fields.

A huge thank you to Tony Gray and the Apple University Consortium (AUC) for the monumental boost they gave us and others listed on this page. We wouldn’t be writing this book if it weren’t for them. And now you’re writing books, too, Tony—sorry about that!

Thanks also to Neal Goldstein, who deserves full credit and/or blame for getting us into the whole book-writing racket.

We’re thankful for the support of the goons at MacLab (who know who they are and continue to stand watch for Admiral Dolphin’s inevitable apotheosis), as well as Professor Christopher Lueg, Dr. Leonie Ellis, and the rest of the staff at the University of Tasmania for putting up with us. “Apologies” to Mark Pesce. He knows why.

Additional thanks to Mars G., Tim N., Dave J., Rex S., Nic W., Andrew B., Jess L., Alec H., Scott B., Bethany H., Adam S., and Rebekah S., for a wide variety of reasons. And very special thanks to the team of hard-working engineers, writers, artists, and other workers at Apple, without whom this book (and many others like it) would not have reason to exist.

Finally, thank you very much for buying our book—we appreciate it! And if you have any feedback, please let us know. You can email us at lab@secretlab.com.au and find us on Twitter at @thesecretlab.

Get iOS Swift Game Development Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.