ptg Download from www.wowebook.com ptg Praise for Learning iOS Game Programming “An excellent introduction into the world of game development explaining every aspect of game design and implementation for the iPad, iPhone, and iPod touch devices. A great way for anyone interested in writing games to get started.” —Tom Bradley, Software Architect, Designer of TBXML “A great developer and a great game.That’s everything you can find in this book to learn how to write an awesome game for iPhone. Maybe you’re the next AppStore hit!” —Sebastien Cardoso “With Learning iOS Game Programming, you’ll be writing your own games in no time. The code included is well explained and will save you hours of looking up obscure stuff in the documentation and online forums.” —Pablo Gomez Basanta, Founder, Shifting Mind “I always thought that to teach others one has to be an expert and a person with an established reputation in the field. Michael Daley proved me wrong. He is teaching oth- ers while studying himself. Michael’s passion in teaching and studying, ease of solutions to problems, and a complete game as a resulting project makes this book one of the best I have ever read.” —Eugene Snyetilov “If you’re interested in 2D game programming with the iOS using OpenGL and OpenAL directly, this book walks you through creating a complete and fun game without getting bogged down in technical details.” —Scott D. Yelich “Michael Daley brings clarity to the haze of iPhone application development. Concrete examples, thorough explanation, and timesaving tips make this book a must have for the up and coming iPhone game developer.” —Brandon Middleton, Creator of Tic Tac Toe Ten “This is the A-Z guide to iOS game development; Michael’s book takes you from the basics and terminology to using the techniques in practice on a fully working game. Before you know it, you will find yourself writing your own game, fueled by a firm grasp of the principles and techniques learned within. I could not ask for a better refer- ence in developing our own games.” —Rod Strougo, Founder Prop Group Download from www.wowebook.com ptg This page intentionally left blank Download from www.wowebook.com ptg Learning iOS Game Programming Download from www.wowebook.com ptg This page intentionally left blank Download from www.wowebook.com ptg Learning iOS Game Programming Michael Daley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City Download from www.wowebook.com ptg Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publish- er was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omis- sions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Library of Congress cataloging-in-publication data is on file. Copyright © 2011 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited repro- duction, storage in a retrieval system, or transmission in any form or by any means, elec- tronic, mechanical, photocopying, recording, or likewise. For information regarding permis- sions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-321-69942-8 ISBN-10: 0-321-69942-4 Text printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville, Indiana. First printing September 2010 Senior Acquisitions Editor Chuck Toporek Senior Development Editor Chris Zahn Managing Editor Kristy Hart Project Editors Barbara Campbell and Jovana San Nicolas-Shirley Copy Editor Water Crest Publishing Indexer Lisa Stumpf Proofreader Sheri Cain Publishing Coordinator Romny French Cover Designer Chuti Prasertsith Download from www.wowebook.com ptg ❖ Dedicated to my mum, Jen ❖ Download from www.wowebook.com ptg Contents at a Glance Preface xxi 1 Game Design 1 2 The Three Ts: Terminology, Technology, and Tools 13 3 The Journey Begins 39 4 The Game Loop 73 5 Image Rendering 97 6 Sprite Sheets 137 7 Animation 153 8 Bitmap Fonts 165 9 Tile Maps 183 10 The Particle Emitter 223 11 Sound 249 12 User Input 285 13 The Game Interface 299 14 Game Objects and Entities 325 15 Collision Detection 357 16 Putting It All Together 369 Index 395 Download from www.wowebook.com ptg Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi 1 Game Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Game That Started It All (For Me) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 So, What’s the Big Idea? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 A Game That Fits with the iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Storyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 What’s in a Name? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Game’s Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Game Play Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Lives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Health . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Doors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Weapons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 The Three Ts: Terminology, Technology, and Tools . . . . . . . . . . . . . . . . . . 13 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Sprite Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Bitmap Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Tile Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Particle System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Artificial Intelligence (AI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Cocoa Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 OpenGL ES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 OpenAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Download from www.wowebook.com [...]... on the task of learning how to build games for the iPhone Michael started writing adventure games in BASIC on a Sinclair Spectrum 48k and progressed onto the Commodore 64 and the Amiga A500 Never having lost a passion for game programming, Michael got inspired to learn Objective-C when the iPhone came out, and he set out to learn how to build games for the iPhone Having written many games for his children... that a number of ideas for this game have come from classic Spectrum games I played in the 1980s I enjoyed them so much, I just could not resist using some of their classic game play within my first public iPhone game A Game That Fits with the iPhone One of the big considerations while working on the concept for the game was how people actually use the iPhone I love playing games on the iPhone, but I don’t... and working on bringing your very own game to life is very much the same This chapter covers the key design principles and game elements that I considered when designing and developing the game that we cover in this book, such as the following: n n n High-level game design Storyline Game play components It’s important that we build a solid base from which the game will grow, and this chapter provides... design.At this point, I also wanted to give the game a name I can then refer to the game using this name going forward, and it helps build the game s identity Download from www.wowebook.com 6 Chapter 1 Game Design You would not think that naming your game could cause problems, but you need to be aware of a couple of important issues when it comes to deploying your game to the App Store.At the time of writing,... the creation of a complete game: Sir Lamorak’s Quest:The Spell of Release.This game is currently available for free download from the App Store, and is the game you learn how to build as you work your way through this book Download the Game! You can download Sir Lamorak’s Quest from the App Store: http://itunes.apple.com/us/app/sir-lamoraks-quest-thespell/id368507448?mt=8 The game is freely available,... to play I spent some time working on the design of the game, which included not only the features I wanted in the game, but also how it should be implemented on the iPhone One key aspect of the game is that it should be casual—that is, the concept of the game should be simple and easy to pick up, and players should be able to start and stop the game easily without losing their progress I also wanted... sets the scene for the chapters that follow Chapter 4, “The Game Loop”—The heartbeat of any game is the game loop.This loop is responsible for making sure that all the core elements of the game, such as AI and rendering, are done at the right time and in the right order.This may sound simple, but there are a number of different approaches to the game loop, and this chapter discusses them and details the... built for Sir Lamorak’s Quest and the key capabilities and considerations are needed to create a 2D game engine.This enables you to take the same game engine developed in this book and use it in your own games, or simply use the knowledge you have gained about creating games in general and use one of the many game engines available for the iPhone, such as Cocos2D Who This Book Is For If you are already... The “Camera” 369 Saving the Game State and Settings 371 Saving Game State 371 Loading Game State 373 Saving Game Settings 375 Loading Game Settings 376 Saving High Scores... needed to start our journey into creating a game on the iPhone Download from www.wowebook.com The Game That Started It All (For Me) 3 The Game That Started It All (For Me) At the time, I was mostly inspired by Matthew Smith, the 17-year-old who created the hit title Manic Miner.1 Manic Miner (see Figure 1.2) was a platform game with 20 levels, and it even had an in -game soundtrack, which was a first back . www.wowebook.com ptg Learning iOS Game Programming Download from www.wowebook.com ptg This page intentionally left blank Download from www.wowebook.com ptg Learning iOS Game Programming Michael. awesome game for iPhone. Maybe you’re the next AppStore hit!” —Sebastien Cardoso “With Learning iOS Game Programming, you’ll be writing your own games in