Preface - iOS Game Development Cookbook (2014)

iOS Game Development Cookbook (2014)

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 namedCulture, led us to working on a 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. If you’re stuck figuring out how to give objects physical motion, how to handle Game Center, or just want a refresher on common gaming-related math problems, you’ll find simple, straightforward answers, explanations, and sample projects. This book is designed as part-way between a series of tutorials and a reference work: it’s something you’ll want to keep handy for quick reference, as well as browse through to get new ideas about what’s possible.

Audience

We assume that you’re a reasonably capable programmer, and assume 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 Objective-C 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, since 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, what 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 different 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 customised to suit your needs—for some kinds of games, you might not need to use any more complex graphical tools than it.

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 their 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.

Chapter 6, 2D Graphics and Sprite Kit

This chapter discusses Sprite Kit, the 2D graphics system built into iOS. Sprite Kit 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 two-dimensional physics simulation that’s provided as part of Sprite Kit. 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, 3D Graphics

This chapter and the two that follow it provide a tutorial on OpenGL ES 2, the 3D graphics system used on iOS. These three chapters follow a slightly different structure to the rest of the book, because it’s not quite as easy to explain parts of OpenGL in isolation. Instead, these chapters follow a more linear approach, and we recommend that you read the recipes in order, so that you can get the best use out of them. Chapter 8 introduces the basics of OpenGL, and shows you how to draw simple shapes on the screen; at the same time, the math behind the graphics is explained.

Chapter 9, Intermediate 3D Graphics

This chapter is designed to follow on from the previous, and discusses more advanced techniques in 3D graphics. You’ll learn how to load a 3D object from a file, how to animate objects, and how to make a camera that moves around in 3D space.

Chapter 10, Advanced 3D Graphics

This chapter follows on from the previous two, and focuses on shaders, which give you a tremendous amout of control over how objects in your game look. You’ll learn how to write shader code, how to create different shading effects (including diffiuse and specular lighting, cartoon shading, and more), and how to make objects transparent.

Chapter 11, 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 an object chase another, make objects flee from something, and how to work out a path from one point to another while avoiding obstacles.

Chapter 12, Networking and Social Media

In this chapter, you’ll learn about Game Center, the social network and match-making system built into iOS. You’ll learn how to get information about the player’s profile, as well as let the player connect to their friends. On top of this, you’ll also learn how to connect to nearby devices using Bluetooth. Finally, you’ll learn how to share text, pictures and other content to social media sites like Twitter and Facebook.

Chapter 13, Game Controllers and External Screens

This chapter discusses the things that players can connect to their device: external displays, like televisions and monitors, and game controllers that provide additional input methods like thumbsticks and physical buttons. You’ll learn how to detect, use and design your game to take advantage of additional hardware where it’s present.

Chapter 14, 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, or fork using GitHub, the code samples from this book at https://github.com/thesecretlab/iOSGameDevCookbook1stEd.

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

§ Physics for Game Developers

§ Learning Cocoa with Objective-C (by us!)

§ Programming iOS 7

We strongly recommend that you add Gamasutra to your regular reading list, due to the high quality of their 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.