Learn iPhone and iPad cocos2d Game Development Copyright © 2010 by Steffen Itterheim All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3303-9 ISBN-13 (electronic): 978-1-4302-3304-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Clay Andres Development Editor: Brian MacDonald Technical Reviewer: Boon Chew Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Kelly Moritz Copy Editors: Sharon Terdeman and Damon Larson Compositor: MacPS, LLC Indexer: John Collin Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. Contents at a Glance ■Contents..........................................................................................................v ■About the Author...........................................................................................xii ■About the Technical Reviewer......................................................................xiii ■Acknowledgments.........................................................................................xiv ■Preface...........................................................................................................xv ■Chapter 1: Introduction...................................................................................1 ■Chapter 2: Getting Started.............................................................................13 ■Chapter 3: Essentials.....................................................................................39 ■Chapter 4: Your First Game...........................................................................65 ■Chapter 5: Game Building Blocks..................................................................93 ■Chapter 6: Sprites In-Depth.........................................................................117 ■Chapter 7: Scrolling with Joy......................................................................141 ■Chapter 8: Shoot ’em Up..............................................................................167 ■Chapter 9: Particle Effects...........................................................................189 ■Chapter 10: Working with Tilemaps............................................................211 ■Chapter 11: Isometric Tilemaps..................................................................235 ■Chapter 12: Physics Engines.......................................................................263 ■Chapter 13: Pinball Game............................................................................287 ■Chapter 14: Game Center.............................................................................315 ■Chapter 15: Out of the Ordinary...................................................................347 ■Index............................................................................................................365 iv Contents ■Contents at a Glance.......................................................................................iv ■About the Author...........................................................................................xii ■About the Technical Reviewer......................................................................xiii ■Acknowledgments.........................................................................................xiv ■Preface...........................................................................................................xv ■Chapter 1: Introduction...................................................................................1 Why Use cocos2d for iPhone?................................................................................................................................2 It’s Free..............................................................................................................................................................2 It’s Open Source.................................................................................................................................................2 It’s Objective, See?............................................................................................................................................2 It’s 2D.................................................................................................................................................................3 It’s Got Physics..................................................................................................................................................3 It’s Less Technical.............................................................................................................................................3 It’s Still Programming........................................................................................................................................4 It’s Got a Great Community................................................................................................................................4 Important cocos2d Tidbits......................................................................................................................................5 Section 3.3.1......................................................................................................................................................5 Porting to Other Platforms.................................................................................................................................5 This Book Is for You................................................................................................................................................6 Prerequisites...........................................................................................................................................................6 Programming Experience..................................................................................................................................6 Objective-C........................................................................................................................................................6 What You Will Learn................................................................................................................................................7 What Beginning iOS Game Developers Will Learn.............................................................................................7 What iPhone App Developers Will Learn............................................................................................................8 What Cocos2d Developers Will Learn................................................................................................................8 v ■ CONTENTS What’s in This Book................................................................................................................................................9 Chapter 2 – Getting Started...............................................................................................................................9 Chapter 3 – Essentials.......................................................................................................................................9 Chapter 4 – Your First Game..............................................................................................................................9 Chapter 5 – Game Building Blocks....................................................................................................................9 Chapter 6 – Sprites In-Depth.............................................................................................................................9 Chapter 7 – Scrolling with Joy...........................................................................................................................9 Chapter 8 – Shoot ’em Up................................................................................................................................10 Chapter 9 – Particle Effects.............................................................................................................................10 Chapter 10 – Working with Tilemaps...............................................................................................................10 Chapter 11 – Isometric Tilemaps.....................................................................................................................10 Chapter 12 – Physics Engines.........................................................................................................................10 Chapter 13 – Pinball Game..............................................................................................................................10 Chapter 14 – Game Center...............................................................................................................................10 Chapter 15 – Conclusion..................................................................................................................................10 Questions & Feedback..........................................................................................................................................11 ■Chapter 2: Getting Started.............................................................................13 What You Need to Get Started..............................................................................................................................13 System Requirements......................................................................................................................................13 Register as iOS Developer...............................................................................................................................14 Certificates & Provisioning Profiles..................................................................................................................14 Download & Install the iPhone SDK.................................................................................................................14 Download & Install cocos2d.............................................................................................................................15 The HelloWorld Application...................................................................................................................................19 Locating the HelloWorld files...........................................................................................................................20 Resources........................................................................................................................................................21 Other Sources..................................................................................................................................................21 Classes.............................................................................................................................................................22 Memory Management with cocos2d.....................................................................................................................27 Changing the World..............................................................................................................................................29 What Else You Should Know.................................................................................................................................31 The iOS Devices...............................................................................................................................................32 About Memory Usage.......................................................................................................................................33 The Simulator...................................................................................................................................................35 About Logging..................................................................................................................................................36 Conclusion............................................................................................................................................................36 ■Chapter 3: Essentials.....................................................................................39 Singletons in cocos2d...........................................................................................................................................39 The Director..........................................................................................................................................................41 The Scene Graph...................................................................................................................................................41 The CCNode Class Hierarchy................................................................................................................................43 CCNode.................................................................................................................................................................43 Working with Nodes.........................................................................................................................................43 Working with Actions.......................................................................................................................................44 Scheduled Messages.......................................................................................................................................45 Scenes and Layers................................................................................................................................................48 CCScene...........................................................................................................................................................49 vi ■ CONTENTS Scenes and Memory........................................................................................................................................49 Pushing and Popping Scenes..........................................................................................................................50 CCTransitionScene...........................................................................................................................................51 CCLayer............................................................................................................................................................52 CCSprite................................................................................................................................................................56 Anchor Points Demystified...............................................................................................................................56 Texture Dimensions.........................................................................................................................................57 CCLabel.................................................................................................................................................................57 Menus...................................................................................................................................................................58 Actions..................................................................................................................................................................60 Repeating Actions............................................................................................................................................61 Ease Actions....................................................................................................................................................61 Action Sequences............................................................................................................................................62 Instant Actions.................................................................................................................................................62 Cocos2d Test Cases..............................................................................................................................................63 Conclusion............................................................................................................................................................63 ■Chapter 4: Your First Game...........................................................................65 Step-By-Step Project Setup..................................................................................................................................66 Adding the Player Sprite.......................................................................................................................................71 Accelerometer Input.............................................................................................................................................74 First Test Run........................................................................................................................................................75 Player Velocity......................................................................................................................................................75 Adding Obstacles..................................................................................................................................................78 Collision Detection................................................................................................................................................84 Score Label...........................................................................................................................................................85 Introducing CCBitmapFontAtlas and Hiero............................................................................................................86 Adding Audio.........................................................................................................................................................89 Porting to iPad......................................................................................................................................................90 Conclusion............................................................................................................................................................91 ■Chapter 5: Game Building Blocks..................................................................93 Working with Multiple Scenes..............................................................................................................................93 Adding More Scenes........................................................................................................................................93 Loading Next Paragraph, Please Stand By.......................................................................................................95 Working with Multiple Layers...............................................................................................................................98 How to Best Implement Levels......................................................................................................................103 CCColorLayer.................................................................................................................................................104 Subclassing Game Objects from CCSprite..........................................................................................................105 Composing Game Objects using CCSprite..........................................................................................................106 Curiously Cool CCNode Classes..........................................................................................................................112 CCProgressTimer...........................................................................................................................................112 CCParallaxNode.............................................................................................................................................113 CCRibbon.......................................................................................................................................................114 CCMotionStreak.............................................................................................................................................115 Conclusion..........................................................................................................................................................116 ■Chapter 6: Sprites In-Depth.........................................................................117 CCSpriteBatchNode.............................................................................................................................................117 When to Use the CCSpriteBatchNode............................................................................................................119 vii ■ CONTENTS Demo Projects................................................................................................................................................119 Sprite Animations the Hard Way.........................................................................................................................124 Animation Helper Category.................................................................................................................................126 Working with Texture Atlases.............................................................................................................................128 What Is a Texture Atlas?................................................................................................................................128 Introducing Zwoptex......................................................................................................................................129 Creating a Texture Atlas with Zwoptex Desktop............................................................................................130 Using the Texture Atlas with Cocos2d...........................................................................................................134 Updating the CCAnimation Helper Category..................................................................................................136 All into One and One for All............................................................................................................................137 Do It Yourself......................................................................................................................................................138 Conclusion..........................................................................................................................................................139 ■Chapter 7: Scrolling with Joy......................................................................141 Advanced Parallax Scrolling...............................................................................................................................141 Creating the Background As Stripes..............................................................................................................141 Re-creating the Background in Code.............................................................................................................144 Moving the ParallaxBackground....................................................................................................................146 Parallax Speed Factors..................................................................................................................................147 Scrolling to Infinity and Beyond.....................................................................................................................149 Fixing the Flicker...........................................................................................................................................151 Repeat, repeat, repeat...................................................................................................................................152 A Virtual Joypad..................................................................................................................................................153 Introducing SneakyInput................................................................................................................................154 Integrating SneakyInput.................................................................................................................................155 Touch Button to Shoot...................................................................................................................................156 Skinning the Button.......................................................................................................................................158 Controlling the Action....................................................................................................................................160 Digital Controls..............................................................................................................................................164 An Alternative: GPJoystick.............................................................................................................................164 Conclusion..........................................................................................................................................................164 ■Chapter 8: Shoot ’em Up..............................................................................167 Adding the BulletCache Class.............................................................................................................................167 What about Enemies?.........................................................................................................................................171 The Entity Class Hierarchy..................................................................................................................................173 The EnemyEntity Class...................................................................................................................................174 The EnemyCache Class..................................................................................................................................177 The Component Classes................................................................................................................................181 Shooting Things..................................................................................................................................................183 A Healthbar for the Boss.....................................................................................................................................185 Conclusion..........................................................................................................................................................188 ■Chapter 9: Particle Effects...........................................................................189 Example Particle Effects.....................................................................................................................................189 Creating a Particle Effect the Hard Way..............................................................................................................192 Variance Properties........................................................................................................................................195 Number of Particles.......................................................................................................................................195 Emitter Duration.............................................................................................................................................196 Emitter Modes................................................................................................................................................196 viii ■ CONTENTS Particle Position.............................................................................................................................................199 Particle Size...................................................................................................................................................199 Particle Direction...........................................................................................................................................199 Particle Lifetime.............................................................................................................................................200 Particle Color.................................................................................................................................................200 Particle Blend Mode.......................................................................................................................................201 Particle Texture..............................................................................................................................................202 Introducing the Particle Designer.......................................................................................................................202 Using Particle Designer Effects......................................................................................................................205 Sharing Particle Effects.................................................................................................................................207 Shoot ’em Up with Particle Effects.....................................................................................................................208 Conclusion..........................................................................................................................................................210 ■Chapter 10: Working with Tilemaps............................................................211 What Is a Tilemap?.............................................................................................................................................211 Preparing Images with Zwoptex.........................................................................................................................214 Tiled Map Editor..................................................................................................................................................216 Creating a New Tilemap.................................................................................................................................216 Designing a Tilemap......................................................................................................................................218 Using Orthogonal Tilemaps with Cocos2d..........................................................................................................221 Locating Touched Tiles..................................................................................................................................224 An Exercise in Optimization and Readability..................................................................................................227 Working with the Object Layer.......................................................................................................................227 Drawing the Object Layer Rectangles............................................................................................................229 Scrolling the Tilemap.....................................................................................................................................232 Conclusion..........................................................................................................................................................233 ■Chapter 11: Isometric Tilemaps..................................................................235 Designing Isometric Tile Graphics......................................................................................................................236 Isometric Tilemap Editing with Tiled...................................................................................................................240 Creating a New Isometric Tilemap.................................................................................................................240 Creating a New Isometric Tileset...................................................................................................................241 Laying Down Some Ground Rules..................................................................................................................242 Isometric Game Programming............................................................................................................................244 Loading the Isometric Tilemap in Cocos2d....................................................................................................244 Setup Cocos2d for Isometric Tilemaps..........................................................................................................244 Locating an Isometric Tile..............................................................................................................................246 Scrolling the Isometric Tilemap.....................................................................................................................248 This World Deserves a Better End..................................................................................................................249 Adding a Movable Player Character...............................................................................................................253 Adding More Content to the Game......................................................................................................................260 Conclusion..........................................................................................................................................................261 ■Chapter 12: Physics Engines.......................................................................263 Basic Concepts of Physics Engines....................................................................................................................263 Limitations of Physics Engines...........................................................................................................................264 The Showdown: Box2D vs. Chipmunk................................................................................................................264 Box2D..................................................................................................................................................................265 The World According to Box2D......................................................................................................................266 Restricting Movement to the Screen.............................................................................................................268 ix ■ CONTENTS Converting Points...........................................................................................................................................270 Adding Boxes to the Box2D World ................................................................................................................271 Connecting Sprites with Bodies....................................................................................................................272 Collision Detection .........................................................................................................................................273 Joint Venture..................................................................................................................................................276 Chipmunk...........................................................................................................................................................277 Objectified Chipmunk....................................................................................................................................277 Chipmunks in Space .....................................................................................................................................278 Boxing-In the Boxes......................................................................................................................................279 Adding Ticky-Tacky Little Boxes...................................................................................................................280 Updating the Boxes’ Sprites..........................................................................................................................282 A Chipmunk Collision Course. .......................................................................................................................283 Joints for Chipmunks. ...................................................................................................................................285 Conclusion .........................................................................................................................................................286 ■Chapter 13: Pinball Game. .......................................................................... 287 Shapes: Convex and Counterclockwise. ............................................................................................................288 Working with VertexHelper ................................................................................................................................289 Creating the Pinball Table...................................................................................................................................291 Box2D Debug Drawing. .................................................................................................................................296 Adding Dynamic Elements. ................................................................................................................................297 The BodyNode Class .....................................................................................................................................298 The Ball. ........................................................................................................................................................301 The Bumpers..................................................................................................................................................305 The Plunger...................................................................................................................................................306 The Flippers ..................................................................................................................................................310 Conclusion .........................................................................................................................................................314 ■Chapter 14: Game Center. ........................................................................... 315 Enabling Game Center .......................................................................................................................................315 Creating Your App in iTunes Connect ...........................................................................................................316 Setting Up Leaderboards and Achievements................................................................................................317 Creating a Cocos2d Xcode Project................................................................................................................317 Configuring the Xcode Project ......................................................................................................................318 Summary .......................................................................................................................................................322 Game Kit Programming......................................................................................................................................323 The GameKitHelper Delegate. .......................................................................................................................323 Checking for Game Center Availability..........................................................................................................324 Authenticating the Local Player ....................................................................................................................325 Block Objects.................................................................................................................................................328 Receiving the Local Player’s Friend List .......................................................................................................330 Leaderboards.................................................................................................................................................332 Achievements ................................................................................................................................................336 Matchmaking.................................................................................................................................................341 Conclusion .........................................................................................................................................................346 ■Chapter 15: Out of the Ordinary. ................................................................. 347 Useful Technologies............................................................................................................................................348 Social Networks.............................................................................................................................................348 Socket Server Technology ............................................................................................................................349 x ■ CONTENTS Ads and Analytics..........................................................................................................................................350 Push Notification Providers............................................................................................................................351 Source Code Projects to Benefit From................................................................................................................351 Sapus Tongue Source Code...........................................................................................................................351 LevelSVG........................................................................................................................................................352 The iPhone RPG Game Kit..............................................................................................................................352 Line-Drawing Game Starterkit.......................................................................................................................353 For Your Reference.............................................................................................................................................353 Working with Publishers.....................................................................................................................................355 Finding Freelancers............................................................................................................................................356 Finding Free Art and Audio.................................................................................................................................357 Finding the Tools of the Trade............................................................................................................................357 Marketing............................................................................................................................................................358 Marketing Your Game and Your Self..............................................................................................................358 Public Relations and Press Releases.............................................................................................................360 Where to Find Help.............................................................................................................................................361 Cocos2d Home Page......................................................................................................................................361 Stack Exchange Network...............................................................................................................................362 Tutorials and FAQs.........................................................................................................................................362 Famous Last Words............................................................................................................................................363 Conclusion..........................................................................................................................................................363 ■Index............................................................................................................365 xi