Tải bản đầy đủ (.pdf) (508 trang)

iphone ipad game development for dummies

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.02 MB, 508 trang )

Neal Goldstein
Jon Manning
Paris Buttfield-Addison
Learn to:
• Build an actual game from start to finish
• Recognize what makes a good game for
A
pple’s mobile devices
• Use Objective-C®, Cocoa®, OpenGL ES
2
.0, and other iOS programming tools
• Connect your app to Facebook, get it
in
to the App Store, and market it
iPhone
®
& iPad

Game Development
Making Everything Easier!

Visit the companion Web site at www.dummies.com/go/
ipadiphonegameprogramming to find plenty of sample
code and other materials to help you create the examples
in this book
Open the book and find:
• The latest and greatest on the
iPad, iPhone 4, and iOS 4.0
• Elements of great game design
and architecture
• Why you need to be a registered


Apple developer
• Why scoring is critical
• How to debug your games
• What provisioning is and why
you need to know
• Important social aspects of
game design
• Key differences between games
on the iPad and the iPhone
Neal Goldstein has a rock-star reputation among iPhone developers.
He wrote iPhone Application Development For Dummies and frequently
speaks at conferences. Jon Manning and Paris Buttfield-Addison are the
founders of Secret Lab, a game design company that builds fun things for
iPhone and iPad when the principals aren’t playing games for research.
$29.99 US / $35.99 CN / £21.99 UK
ISBN 978-0-470-59910-5
Programming/Apple/Mobile Device
Go to Dummies.com
®
for videos, step-by-step examples,
how-to articles, or to shop!
Start a fun hobby or a
new career — create cool games
for the hottest devices around!
Got an idea for a fun iPad or iPhone game but don’t know
what to do with it? Read this book and you will! You’ll see
how to design games that play wonderfully on the small
screen, what’s involved in becoming a registered developer,
how to work with the SDK, key features of game architecture,
how to market your games, and more. Get your game on!

• What makes a good game? — learn the fundamentals of good
game design
• Put on your developer’s hat — become a registered Apple
developer, learn how iOS games work, and understand
the SDK
• The stuff games are made of — build a native user interface,
explore game architecture fundamentals, learn to animate
objects in iOS, and grasp the basics of OpenGL
• Keep score and get social — set up a scoring system and learn to
tie your game into social networks like Facebook
• To market, to market — get your game into the App Store and
boost marketability with such bonus features as gestures,
shaking, external displays, and ad-supported revenue
iPhone
®
& iPad

Game Development
Goldstein
Manning
Buttfield-Addison
spine=1.01”
www.it-ebooks.info
spine=1.01”
Mobile Apps
There’s a Dummies App for This and That
With more than 200 million books in print and over 1,600 unique
titles, Dummies is a global leader in how-to information. Now
you can get the same great Dummies information in an App. With
topics such as Wine, Spanish, Digital Photography, Certification,

and more, you’ll have instant access to the topics you need to
know in a format you can trust.
To get information on all our Dummies apps, visit the following:
www.Dummies.com/go/mobile from your computer.
www.Dummies.com/go/iphone/apps from your phone.
Start with FREE Cheat Sheets
Cheat Sheets include
• Checklists
• Charts
• Common Instructions
• And Other Good Stuff!
Get Smart at Dummies.com
Dummies.com makes your life easier with 1,000s
of answers on everything from removing wallpaper
to using the latest version of Windows.
Check out our
• Videos
• Illustrated Articles
• Step-by-Step Instructions
Plus, each month you can win valuable prizes by entering
our Dummies.com sweepstakes. *
Want a weekly dose of Dummies? Sign up for Newsletters on
• Digital Photography
• Microsoft Windows & Office
• Personal Finance & Investing
• Health & Wellness
• Computing, iPods & Cell Phones
• eBay
• Internet
• Food, Home & Garden

Find out “HOW” at Dummies.com
*Sweepstakes not currently available in all countries; visit Dummies.com for official rules.
Get More and Do More at Dummies.com
®
To access the Cheat Sheet created specifically for this book, go to
www.dummies.com/cheatsheet/iphoneandipadgamedevelopment
www.it-ebooks.info
by Neal Goldstein, Paris Butt eld-Addison,
and Jon Manning
iPhone
®
& iPad


Game Development
FOR
DUMmIES

01_599105-ffirs.indd i01_599105-ffirs.indd i 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
iPhone
®
& iPad

Game Development For Dummies
®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774

www.wiley.com
Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley
& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://
www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest
of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier,

and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its af liates
in the United States and other countries, and may not be used without written permission. iPhone and iPad are
registered trademarks of Apple Inc. All other trademarks are the property of their respective owners. Wiley
Publishing, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF
THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH-
OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF
A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZA-
TION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE

OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES
THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT
MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS
WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND
WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2010937820
ISBN: 978-0-470-59910-5
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
01_599105-ffirs.indd ii01_599105-ffirs.indd ii 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
About the Authors
Neal Goldstein is a recognized leader in making state-of-the-art, cutting-edge
technologies practical for commercial and enterprise development. He was
one of the  rst technologists to work with commercial developers at  rms
such as Apple Computer, Lucas lm, and Microsoft to develop commercial
applications using object-based programming technologies. He was a pioneer
in moving that approach into the corporate world for developers at Liberty
Mutual Insurance, USWest (now Verizon), National Car Rental, EDS, and
Continental Airlines, showing them how object-oriented programming could
solve enterprise-wide problems. His book (with Jeff Alger) on object-oriented
development, Developing Object-Oriented Software for the Macintosh (Addison
Wesley), introduced the idea of scenarios and patterns to developers. He
was an early advocate of the Microsoft .NET framework, and he successfully
introduced it into many enterprises, including Charles Schwab. He was one of

the earliest developers of Service Oriented Architecture (SOA), and as Senior
Vice President of Advanced Technology and the Chief Architect at Charles
Schwab, he built an integrated SOA solution that spanned the enterprise,
from desktop PCs to servers to complex network mainframes. (He holds
three patents as a result.) As one of IBM’s largest customers, he introduced
the folks at IBM to SOA at the enterprise level and encouraged them to head
in that direction.
He is currently passionate about the real value mobile devices can pro-
vide and has eight applications in the App Store. These include a series of
Travel Photo Guides () developed with
his partners at mobilefortytwo and a Digital Field Guides series (http://
lp.wileypub.com/DestinationDFGiPhoneApp) developed in partner-
ship with John Wiley & Sons. He also has a cool little, free app — Expense
Calendar — that allows you to keep track of things like expenses, mileage,
and time by adding them to your calendar.
Along with those apps, he has written several books on iPhone program-
ming, including iPhone Application Development For Dummies (both edi-
tions) and Objective-C For Dummies, and he coauthored (with Tony Bove)
iPhone Application Development All-In-One For Dummies and iPad Application
Development For Dummies. He is also the coauthor (with Jon Manning and
Paris Butt eld-Addison) of a forthcoming book on using the Unity platform
for game development.
Because you can never tell what he’ll be up to next, check regularly at his
Web site: www.nealgoldstein.com. You can also check out his Facebook
page at www.facebook.com/nealgoldsteinbooks and follow him on
Twitter at www.twitter.com/nealgoldstein.
01_599105-ffirs.indd iii01_599105-ffirs.indd iii 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
Jon Manning has a collection of careers, which he swaps out as neces-
sary: He’s co-founder of Secret Lab (the world’s most dangerous mobile

games startup), a Senior Software Engineer at Meebo, Inc. (where he builds
mobile apps that reach a stupidly huge number of people), and a Graduate
Researcher in Human-Computer Interaction at the University of Tasmania
in Australia (a land of computers and kangaroos). When he isn’t working on
apps or books, he’s working on adding more letters to the end of his name.
He spends nowhere near enough time around cats. You can  nd Jon online at
www.desplesda.net and on Twitter at www.twitter.com/desplesda.
Paris Buttfi eld-Addison wears many hats: He’s co-founder of Secret Lab
(www.secretlab.com.au), author, educator, and Product Manager
(Mobile) at Meebo, Inc., one of the Web’s fastest growing consumer Internet
companies (www.meebo.com). Paris has degrees in HCI, computer science,
and medieval and modern history. He enjoys designing, producing, and build-
ing awesome experiences for mobile devices. Through some miracle of time
management, he is also a Graduate Researcher in information management at
the University of Tasmania, Australia. You can  nd Paris on the Web at www.
paris.id.au and on Twitter at www.twitter.com/parisba.
01_599105-ffirs.indd iv01_599105-ffirs.indd iv 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
Dedication
Neal Goldstein: To my children Evan and Sarah, and all my personal and
artist friends who have kept me centered on the (real) world outside of writ-
ing and technology. But most of all, to my wife Linda, who is everything that
I ever hoped for and more than I deserve. Yes, Sam . . . the light at the end of
the tunnel is not a freight train.
Jon Manning: To my family, for introducing me to this whole “computers” thing.
Paris Buttfi eld-Addison: To my mother and father, for all the usual things —
everything.
Authors’ Acknowledgments
There is no better acquisitions editor than Katie Feltman, who did a superb
job of keeping us on track and doing whatever she needed to do to us to stay

focused on writing. Linda Morris did a great job in the early stages of the
project, and project editor Pat O’Brien made sure all the parts were pulled
together. Also thanks to copy editor Jen Riggs and technical editor Erick
Tejkowski for helping us make things clearer. Thanks again to our agent Carole
Jelen for her continued work and support in putting together these projects.
01_599105-ffirs.indd v01_599105-ffirs.indd v 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at . For
other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, out-
side the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and
Media Development
Project Editor: Pat O’Brien
Acquisitions Editor: Katie Feltman
Copy Editor: Jen Riggs
Technical Editor: Erick Tejkowski
Editorial Manager: Kevin Kirschner
Media Development Assistant Project
Manager: Jenny Swisher
Media Development Associate Producers:
Josh Frank, Marilyn Hummel, Douglas
Kuhn, and Shawn Patrick
Editorial Assistant: Amanda Graham
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant
(www.the5thwave.com)
Composition Services
Project Coordinator: Sheree Montgomery

Layout and Graphics: Timothy C. Detrick,
Joyce Haughey, Andrea Hornberger
Proofreaders: Laura Albert, Shannon Ramsey
Indexer: BIM Indexing & Proofreading Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Composition Services
Debbie Stailey, Director of Composition Services
01_599105-ffirs.indd vi01_599105-ffirs.indd vi 10/1/10 11:15 PM10/1/10 11:15 PM
www.it-ebooks.info
Contents at a Glance
Introduction 1
Part I: Getting Started 7
Chapter 1: Building Great iOS Games 9
Chapter 2: Becoming an iPhone Developer 25
Chapter 3: Your First Date with the SDK 41
Part II: Traffic, The Game 61
Chapter 4: How iOS Games Work 63
Chapter 5: Building the User Interface 81
Chapter 6: Making Objects Appear and Move 99
Chapter 7: The Life Cycle of an iOS Game 123
Chapter 8: Creating the Game Architecture 135
Chapter 9: Creating the Game Controller 145
Chapter 10: Using the Debugger 185
Chapter 11: Keeping Score in Your Game 201

Chapter 12: Storing User Preferences 217
Chapter 13: Death, Taxes, and iOS Provisioning 237
Chapter 14: Giving Your Game Music and Sound 261
Part III: The Social Aspects 277
Chapter 15: Building Multiplayer Games with Game Kit 279
Chapter 16: Game, Meet Facebook 303
Chapter 17: External Displays 325
Chapter 18: iAd 339
Part IV: The iPad 347
Chapter 19: The World of the iPad 349
Chapter 20: Adding Multiple Lanes for the iPad 359
Chapter 21: Using Gesture Recognizers 369
Chapter 22: Setting Up OpenGL 379
Chapter 23: Drawing with OpenGL 403
Chapter 24: Texturing with OpenGL 419
Chapter 25: Kicking Up Your Game a Notch 435
02_599105-ftoc.indd vii02_599105-ftoc.indd vii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
Part V: The Part of Tens 445
Chapter 26: Ten Differences between the iPhone and the iPad 447
Chapter 27: Ten Ways to Market Your Game 451
Chapter 28: Ten Insanely Great Games 457
Index 461
02_599105-ftoc.indd viii02_599105-ftoc.indd viii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
Table of Contents
Introduction 1
About This Book 2
Conventions Used in This Book 3
Foolish Assumptions 3

How This Book Is Organized 4
Part I: Getting Started 4
Part II: Traf c, The Game 4
Part III: The Social Aspects 5
Part IV: The iPad 5
Part V: The Part of Tens 5
Icons Used in This Book 5
Where to Go from Here 6
Part I: Getting Started 7
Chapter 1: Building Great iOS Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Figuring Out What a User Wants from an iPhone Game 10
Establishing a Game Developer Mindset 11
Noting the Features of Good Games 12
Device-guided design 13
Incorporating the fun 14
Designing a Good Game 15
Beginning with an idea 16
Making the idea fun, feasible, and unique 16
Evolving the Game 17
Prototyping on paper 17
Distilling the ingredients of fun 21
Applying Sid Meier’s Rule of Halves 22
What’s Next 23
Chapter 2: Becoming an iPhone Developer. . . . . . . . . . . . . . . . . . . . . . .25
Becoming a Registered iPhone Developer 26
Exploring the iPhone Dev Center 28
Looking forward to using the SDK 30
Resources on the iPhone Dev Center 32
Downloading the SDK 33
Joining the iPhone Developer Program 34

Getting Ready for the SDK 38
02_599105-ftoc.indd ix02_599105-ftoc.indd ix 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
iPhone & iPad Game Development For Dummies
x
Chapter 3: Your First Date with the SDK . . . . . . . . . . . . . . . . . . . . . . . . .41
Developing with the SDK 41
Creating Your Project 42
Exploring Your Project 45
Building and Running Your Application 49
Working with the iPhone Simulator 51
Imitating hardware interaction 51
Emulating gestures 52
Uninstalling applications and resetting your device 53
Customizing Xcode 55
Using Interface Builder 57
Part II: Traffic, The Game 61
Chapter 4: How iOS Games Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Using Frameworks 64
Using Design Patterns 65
The Model-View-Controller (MVC) pattern 66
The MVC in action 68
Working with Windows and Views 69
Looking out the window 69
Admiring the view 69
Exploring the kinds of views you use 72
Controlling View Controllers 74
Adding Your Own Application’s Behavior 76
The Delegation pattern 77
The Target-Action pattern 78

Moving Ahead with Your Game 80
Chapter 5: Building the User Interface. . . . . . . . . . . . . . . . . . . . . . . . . . .81
Building the Main Screen 82
Opening the project 82
Creating the  rst screen 83
Making the buttons look fancy 89
Adding Icons and Launch Screens 92
Adding the icon 94
Adding the launch screen 95
Renaming the view controller 96
Chapter 6: Making Objects Appear and Move . . . . . . . . . . . . . . . . . . . .99
Drawing with Views 99
How Drawing Works on iOS 102
Building the Traf c View Controller 104
Properties 105
Actions and outlets 106
Setting up the animations 108
02_599105-ftoc.indd x02_599105-ftoc.indd x 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
xi
Table of Contents
Creating Cars for the Game 111
Adding an image view subclass 111
Adding the car to the view 114
Using Core Animation to Animate Buttons 116
Understanding layers 116
Working with CAKeyframeAnimation 117
Making the buttons bounce 119
Chapter 7: The Life Cycle of an iOS Game . . . . . . . . . . . . . . . . . . . . . . .123
Starting with the Main Nib File 123

Initialization 124
Event processing 125
Termination 125
Considering Other Runtime Issues 127
Responding to interruptions 127
Managing memory on the iPhone 128
Dealing with Touch Input 129
The long and storied life of a UITouch 129
The stages of a touch 130
Responding to Gestures 132
Processing Touches 133
Chapter 8: Creating the Game Architecture . . . . . . . . . . . . . . . . . . . . .135
Putting on Your Architect’s Hat 135
View controllers, screens, and features 135
Game loops 136
Timers and frame rates 137
Game objects and your app 138
Designing the Screen Flow 139
The main menu 139
The game screen 140
The pause screen 141
The game over screen 142
The high scores screen 143
The settings screen 143
Chapter 9: Creating the Game Controller. . . . . . . . . . . . . . . . . . . . . . . .145
Creating the Traf c Controller Files 146
Registering Cars 147
Creating the Game Timer 150
Setting up the View Controller 152
Keeping Track of the Clock 153

Creating Cars from Lanes 155
Registering lanes 155
Creating new cars 158
Moving Cars Around 161
Updating car positions 161
Moving between lanes 162
02_599105-ftoc.indd xi02_599105-ftoc.indd xi 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
iPhone & iPad Game Development For Dummies
xii
Removing Cars from the Game Board 165
Earning More Time 170
Detecting Collisions 172
Updating the Counter 174
Pausing the Game 177
Creating the Pause view 177
Pausing 179
Making the Game End 181
Creating the game over view 181
Handling the Game Over event 182
Chapter 10: Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Figuring Out What Debugger Can Help You Do 185
Using the Debugger 188
Debugging your project 189
Using the Debugger window 192
Using Breakpoints 194
Using the Static Analyzer 196
Chapter 11: Keeping Score in Your Game . . . . . . . . . . . . . . . . . . . . . . .201
Score-Keeping in Traf c 201
Saving Scores 203

Storing  les in chroot jails 203
File formats 205
Storing the Scores 206
Creating the scores list 207
Loading the scores list 207
Working with Scores 208
Adding scores to the list 209
Keeping score in Traf cController 210
Displaying the High Scores List 212
Creating the scores screen 213
Making scores visible to the player 213
Chapter 12: Storing User Preferences . . . . . . . . . . . . . . . . . . . . . . . . . .217
Designing Settings 217
Figuring out what settings to add 218
Giving the user control(s) 218
Getting Familiar with NSUserDefaults 219
Providing default values 221
Reading settings 222
Writing settings 222
Building Custom Settings Interfaces 223
Using controls 223
Exploring the Target-Action pattern 223
Building Settings in Traf c 225
Adding the variables 225
Loading the default settings 226
02_599105-ftoc.indd xii02_599105-ftoc.indd xii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
xiii
Table of Contents
Saving the settings on exit 227

Constructing the view controller 229
Building the Settings Interface 231
Building the interface 232
Coding the SettingsViewController Class 232
Connecting all of the code 235
Chapter 13: Death, Taxes, and iOS Provisioning . . . . . . . . . . . . . . . . .237
How the Process Works 238
The Distribution process 238
The Development process 239
Provisioning Your Device for Development 241
Getting your Development Provisioning Pro le and
iPhone Development Certi cate 243
Provisioning Your Application for the App Store 246
Getting Your Content in the App Store via iTunes Connect 251
Manage Users 253
Contract, Tax & Banking Information 253
Uploading your information 254
What you need to get your game into the App Store 255
Avoiding the App Store Rejection Slip 259
Now What? 260
Chapter 14: Giving Your Game Music and Sound. . . . . . . . . . . . . . . . .261
Recognizing the Purpose of Sound in a Game 262
What music does 262
What sound effects do 263
Understanding Playback on iOS 263
Uncompressed audio 263
Compressed audio 264
Media playback on iOS 264
Playing Background Music 265
AVAudioPlayer 265

Loading the content 266
Playing Sound Effects 269
Audio ToolBox and system sounds 270
Playing background audio 274
Part III: The Social Aspects 277
Chapter 15: Building Multiplayer Games with Game Kit . . . . . . . . . .279
Understanding the Basics of Game Kit 280
Designing a Multiplayer Version of a Single-Player Game 281
Competitive multiplayer 281
Cooperative multiplayer 282
02_599105-ftoc.indd xiii02_599105-ftoc.indd xiii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
iPhone & iPad Game Development For Dummies
xiv
Picking the paradigm 282
Communicating between players 284
Updating the interface 285
Setting Up the Session 286
Adding the framework to the project 286
Adding the button 288
Starting the session 290
Testing your game on multiple devices 292
Enabling In-Game Communication 293
Archiving objects 294
Constructing the message’s NSData object 294
Handling interruptions 297
Sending Extra Time 298
Chapter 16: Game, Meet Facebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Looking at Facebook 304
Exploring the Uses of Facebook 305

Working with Facebook 306
Obtaining the Facebook SDK 308
Facebook sessions and permissions 309
Adding the Facebook iOS SDK to your project 310
Setting up the application delegate 311
Connecting to Facebook 312
Checking for Permission 314
Logging into Facebook 316
Posting to Facebook 316
Creating the interface 317
Posting an update 318
Testing Everything 319
Improving the User Experience 321
Disabling the buttons 321
Showing activity 322
Chapter 17: External Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
Doubling the Fun with an External Display 326
Looking at Screens, Windows, and Views 327
Detecting extra displays 328
Differentiating among screens 329
Running different screen modes 329
Using the extra display 330
Adapting Your Game 331
Creating the window 331
Sending the output 333
Chapter 18: iAd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Using iAd 339
Joining the iAd Network 340
02_599105-ftoc.indd xiv02_599105-ftoc.indd xiv 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info

xv
Table of Contents
ADBannerView 342
Implementing iAd 343
Conform to My Protocol, Baby 345
Part IV: The iPad 347
Chapter 19: The World of the iPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
Introducing the iPad 349
Discovering the New Rules for iPad Apps 350
Multiple orientations 350
More room for hands 351
Two people, one device 351
Adapting Traf c for the iPad 351
Transitioning the project 351
Deciding how to transition 352
Resizing the views and menus 353
Managing multiple targets 355
High-resolution Screens 356
The Retina display 356
Adding a better default image 357
Chapter 20: Adding Multiple Lanes for the iPad. . . . . . . . . . . . . . . . . .359
Designing Game Play for a Larger Screen 359
Creating Additional Lanes and Multidirectional Cars 362
Adding extra lanes 362
Creating cars 363
Chapter 21: Using Gesture Recognizers. . . . . . . . . . . . . . . . . . . . . . . . .369
Understanding Gestures 369
Detecting gestures 370
Exploring the states of a gesture recognizer 370
Separating gesture detection from action 371

Adding the Gesture Recognizer Code to Your Game 372
Adding the view 372
Responding to the gesture 375
Slowing down time 376
Chapter 22: Setting Up OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
“Behold, the Third Dimension!” 379
Working with 3D space 380
A history lesson 381
Tiny graphics powerhouses 382
How OpenGL Works 383
Contexts 383
Primitives 383
02_599105-ftoc.indd xv02_599105-ftoc.indd xv 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
iPhone & iPad Game Development For Dummies
xvi
Vertices 385
Rasterizing 386
Buffers 386
Shaders 387
Drawing stuff on the screen 389
OpenGL objects, names, and binding 389
Using OpenGL in iOS 390
Core Animation layers 391
EAGLContext, a fountain of mysteries 391
Displaying the frame buffer 391
Setting Up OpenGL for Traf c 392
Setting up the view 392
Creating the 3D view 392
What are these “buffers” of which you speak? 394

Creating the OpenGL context 396
Preparing the buffers 396
Rendering the ‘scene’ 400
Chapter 23: Drawing with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403
The Background Scene 403
The goal 404
Setting up the background rendering 404
Writing Fragment and Vertex Shaders 406
Uniforms, varyings, and attributes — oh my! 406
The vertex shader 407
The fragment shader 408
Tying it all together 409
Drawing the Scene 415
Setting up the vertex arrays 416
Drawing the scene 416
Chapter 24: Texturing with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . .419
Figuring Out How Textures Work 419
Compressing Textures 421
Loading Textures 422
Updating the Shaders 425
Drawing the Texture 428
Making the Road Move 431
Chapter 25: Kicking Up Your Game a Notch . . . . . . . . . . . . . . . . . . . . .435
Accessing the iPod Library 435
Media items, media pickers, and music players 436
Importing the framework 436
Adding the user interface 436
Detecting Shakes 439
Detecting shake events 439
Clearing the screen 440

02_599105-ftoc.indd xvi02_599105-ftoc.indd xvi 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
xvii
Table of Contents
Part V: The Part of Tens 445
Chapter 26: Ten Differences between the iPhone and the iPad . . . .447
The iPad Is Social 447
The iPhone Is Personal 448
The iPad Offers More Direct Control 448
You Can Play the iPad Really Loud 448
iPhone Users Often Wear Headphones 448
Games Can’t Always Be Easily Scaled from iPad to iPhone 449
Users Expect More from an iPad Game 449
The iPhone Is Used in High-Distraction Environments 449
Users Spend More Time Playing iPad Games 450
The iPhone Is Highly Portable and Moveable 450
Chapter 27: Ten Ways to Market Your Game . . . . . . . . . . . . . . . . . . . .451
FriendTube, Tweetfeed, YouFace: Engage with Social Media 452
Give Away Your Game 453
Get Your Game Reviewed 453
Create a Press Kit 453
Use Push Noti cations 454
Offer In App Purchase 454
Solicit iTunes App Store Reviews 455
Watch Your Sales Closely 455
Use Analytics 455
Make Your Web Site Awesome 456
Chapter 28: Ten Insanely Great Games . . . . . . . . . . . . . . . . . . . . . . . . .457
Canabalt 457
Flight Control 458

Plants vs. Zombies 458
Ramp Champ 458
DoodleJump 458
Frenzic 459
Pocket God 459
Words With Friends 459
Chopper (and Chopper 2) 460
Tap Tap Radiation 460
Index 461
02_599105-ftoc.indd xvii02_599105-ftoc.indd xvii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
iPhone & iPad Game Development For Dummies
xviii
02_599105-ftoc.indd xviii02_599105-ftoc.indd xviii 10/1/10 11:28 PM10/1/10 11:28 PM
www.it-ebooks.info
Introduction
O
n September 1, 2010, Steve Jobs took the stage in San Francisco to
announce the latest and greatest iPod. During his speech, he gloated
an astonishing statistic that Apple has 50 percent of the mobile gaming
market; he followed up with the even more astonishing statistic that the iPod
touch alone outsells both Nintendo’s mobile products and Sony’s mobile
products — combined.
We don’t really need to say any more than that to convince you that
developing games for Apple’s mobile platforms is worthwhile! We probably
will though.
As you continue to explore the iOS as a gaming platform, you’ll be amazed
at the possibilities for simple, quick attention-grabbing games that last for
mere moments of time. The iPhone and iPad are, in addition to being useful
and powerful mobile computers, the most interesting gaming devices in

recent memory. The combination of powerful hardware, a decent display,
permanent Internet connectivity, and an enormous user base (120 million
iOS devices as of late 2010) makes it possible to create a class of games for
mobile users that were once possible only on desktop PCs. But not only
that — Apple’s App Store provides a direct sales and distribution channel to
potential users that really can’t be beaten.
The iPhone and iPad, and iOS in general, are game changers for the world of
game development. Never before has it been so easy for an individual, or a
small group, to build a game that can be distributed to the world. One of the
hallmarks of a great iOS game is that it leverages the unique hardware and
operating system (iOS) that Apple produces for an extremely polished and
consistent, yet unique, experience. The iOS Software Development Kit (SDK),
which you use to develop iOS games, includes tools such as OpenGL ES, which
makes the type of 3D graphics that would’ve never been possible on a mobile
device, dare we say it, simple. The frameworks supplied in the SDK are espe-
cially rich and mature. All you really have to do is add your game’s user inter-
face and game play mechanics to the framework, and then poof . . . an instant
game. Well, sort of — but we help you through the patches that are a bit more
challenging, as we guide you along the way to making an awesome game.
03_599105-intro.indd 103_599105-intro.indd 1 10/1/10 11:33 PM10/1/10 11:33 PM
www.it-ebooks.info
2
iPhone & iPad Game Development For Dummies
If you’re familiar with older versions of the SDK, you’re in for a pleasant
surprise: SDK version 4.1, which includes Xcode 3.2.3, is a lot, lot better and
easier to use. This book is based on iOS 4.1 for iPhone and iOS 3.2 for iPad
(the latest versions at the time of writing) and Xcode 3.2.3.
If this seems too good to be true, well, okay, it is, sort of. What’s really hard,
after you figure out the language and framework, is how to create a program
structure for an iOS. Although there are lots of resources, the problem is

exactly that: There are lots of resources — as in thousands of pages of docu-
mentation! You may get through a small fraction of the documentation before
you just can’t take it anymore and plunge right into coding. Naturally enough,
you’ll have a few false starts and blind alleys until you find your way, but we
predict that after reading this book, it’s (pretty much) smooth sailing.
About This Book
iPhone & iPad Game Development For Dummies is a beginner’s guide to devel-
oping games for the iPhone, iPod touch, and iPad, which all run Apple’s iOS.
And not only do you not need any iPad (or iPhone) development experience
to get started, but you also don’t need any Macintosh development experi-
ence either. We expect you to come as a blank slate, ready to be filled with
useful information and new ways to do things.
The iOS devices allow you to build truly innovative, simple, and clever
games that can reach a wider audience than was ever possible for indepen-
dently developed games in the past. And because you can also start small
and create fun, simple games that entertain the player, it’s relatively easy
to transform yourself from “you know nothing” into a game developer who,
though not (yet) a superstar, can still crank out quite a respectable game.
The iPhone and iPad devices can be home to some pretty fancy games as
well — so we take you on a journey through building not just a simple game
but also a souped up version for the iPad that uses OpenGL ES (a 3D graphics
system that we cover in Chapter 22) so that you know the ropes for develop-
ing your own game.
This book distills the hundreds (or even thousands) of pages of Apple docu-
mentation, not to mention our own game and app development experiences,
into only what’s necessary to start developing real, fun games. But this is no
recipe book that leaves it up to you to put it all together; rather, we take you
through the frameworks and iOS architecture in a way that gives you a solid
foundation in how games really work on the iPhone, iPad, and iPod touch.
This book acts as a roadmap to expand your knowledge as you need to.

This book is a multiple-course banquet, intended to make you feel satisfied
(and really full) at the end.
03_599105-intro.indd 203_599105-intro.indd 2 10/1/10 11:33 PM10/1/10 11:33 PM
www.it-ebooks.info
3

Introduction
Conventions Used in This Book
This book guides you through the process of building iOS games. Throughout,
you use the provided iOS framework classes for iOS (and create new ones, of
course) and code them using the Objective-C programming language.
Code examples in this book appear in a monofont so they stand out a bit
better. That means the code you see looks like this:
#import <UIKit/ UIKit.h>
Objective-C is based on C, which (we want to remind you) is case-sensitive,
so please enter the code that appears in this book exactly as it appears in the
text. This book also uses the standard Objective-C naming conventions —
for example, class names always start with a capital letter, and the names of
methods and instance variables always start with a lowercase letter.
All URLs in this book appear in a monofont as well:
www.nealgoldstein.com
If you’re ever uncertain about anything in the code, you can always look at
the source code on the Internet at www.nealgoldstein.com or www.
traffic.secretlab.com.au. (You can grab the same material from the
For Dummies Web site at www.dummies.com/go/PONIES.) From time to
time, we provide updates for the code there and post other things you might
find useful. Neal also offers insights about everything from developing apps
to the future of mobile devices and applications at www.nealgoldstein.
com. Secret Lab also posts articles and notes on game design and develop-
ment at www.secretlab.com.au.

Foolish Assumptions
To begin creating your iOS games, you need an Intel-based Macintosh com-
puter with the latest version of the Mac OS on it. (No, you can’t program
iPhone applications on the iPad!) You also need to download the iOS SDK —
which is free — but you have to become a Registered iOS Developer before
you can do that. (Don’t worry; we show you how in Chapter 2.) And, oh yeah,
you need an iPhone or iPod touch (or an iPad if that’s your target device).
You don’t run your game on them right away — you use the Simulator that
Apple provides with the iOS SDK during the initial stages of development —
but at some point, you need to test your application on a real, live iOS device.
03_599105-intro.indd 303_599105-intro.indd 3 10/1/10 11:33 PM10/1/10 11:33 PM
www.it-ebooks.info
4
iPhone & iPad Game Development For Dummies
This book assumes that you have some programming knowledge and that
you have at least a passing acquaintance with object-oriented programming,
using some variant of the C language (such as C++, C#, or even Objective-C).
If not, we point out some resources that can help you get up to speed (includ-
ing Neal’s book, Objective-C For Dummies). The examples in this book focus
on the frameworks that come with the SDK; the code is pretty simple (usu-
ally) and straightforward. (We don’t use this book as a platform to dazzle you
with fancy coding techniques.)
This book also assumes that you’re familiar with the iPhone and iPad, and
that you’ve at least explored Apple’s included applications to get a good
working sense of the iOS look, feel, and style. Browse the App Store to see the
kinds of games available there, and maybe even download a few free ones (as
if we could stop you).
How This Book Is Organized
iPhone & iPad Game Development For Dummies has five main parts, which we
explain in more detail in the following sections.

Part I: Getting Started
Part I introduces you to the iOS game development world. You find out what
makes a great iOS game, and how to exploit the iPhone, iPod touch, and
iPad’s best features to create a compelling and fun gaming experience. You
also discover how to sign up for the iOS Developer Program and become an
official developer so that you can distribute your games through the App
Store. You also explore the components of the iOS SDK, such as Xcode (the
Apple development environment) and Interface Builder.
Part II: Traffic, The Game
In this part, you find out how iPhone games work, and we explain how to use
the frameworks that form the raw material of any iOS app to assemble the
user interface of our example game, Traffic, and to move things around on the
screen. We also reveal design patterns that you need to adopt to make use of
the iOS SDK. Part II also describes how to debug your games, provision your
work for testing on real devices (and for distribution to the App Store), and
play music and sounds.
03_599105-intro.indd 403_599105-intro.indd 4 10/1/10 11:33 PM10/1/10 11:33 PM
www.it-ebooks.info
5

Introduction
Part III: The Social Aspects
Part III is deceptively short but intensely illuminating. These four chapters
describe integrating more social technologies with your game, including
Apple’s Game Kit framework for wireless networking among people on mul-
tiple devices, Facebook for posting social updates, and external display sup-
port for making your game have more of a party atmosphere.
Part IV: The iPad
With the basics behind you and a good understanding of the iPhone game
architecture under your belt, it’s time to talk about money and the iPad. In

this part, we discuss Apple’s iAd for generating revenue through the sale of
advertising displayed in your game. Part IV also covers the theoretical and
practical aspects of upsizing your game to the world of high-resolution and
technologies introduced with the iPhone 4 and the iPad, such as gesture rec-
ognizers. We dip a toe in the world of OpenGL ES (Apple’s fast 3D graphics
library) and speed up the world of Traffic with some new effects and features
for the iPad version.
Part V: The Part of Tens
Part V consists of some tips to help you avoid figuring out everything the
hard way. We talk about some key differences to consider when designing
games for the iPad and iPhone, discuss some marketing tips to help get you
on the road to App Store success, and showcase our ten favorite games to be
inspired by.
Icons Used in This Book

This icon indicates a useful point that you shouldn’t skip.

This icon represents a friendly reminder. We describe a vital point here that
you should keep in mind while proceeding through a particular section of the
chapter.
03_599105-intro.indd 503_599105-intro.indd 5 10/1/10 11:33 PM10/1/10 11:33 PM
www.it-ebooks.info

×