Learning Cocos2D
A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk
Rod Strougo
Ray Wenderlich
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
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 publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors 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 omissions. 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 purchases 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
For sales outside the United States please contact:
International Sales
Visit us on the Web: informit.com/aw
Editor-in-Chief
Mark Taub
Acquisitions Editor
Chuck Toporek
Managing Editor
John Fuller
Project Editor
Anna Popick
Copy Editor
Carol Lallier
Indexer
Jack Lewis
Proofreader
Lori Newhouse
Editorial Assistant
Olivia Basegio
Cover Designer
Chuti Prasertsith
Compositor
The CIP Group
Library of Congress Cataloging-in-Publication Data
Strougo, Rod, 1976
Learning Cocos2D : a hands-on guide to building iOS games with
Cocos2D, Box2D, and Chipmunk / Rod Strougo, Ray Wenderlich.
p. cm.
Includes index.
ISBN-13: 978-0-321-73562-1 (pbk. : alk. paper)
ISBN-10: 0-321-73562-5 (pbk. : alk. paper)
1. iPhone (Smartphone)Programming. 2. iPad (Computer)Programming.
3. Computer gamesProgramming. I. Wenderlich, Ray, 1980- II. Title.
QA76.8.I64S87 2011
794.81526dc23
2011014419
Copyright 2012 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 reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, 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-73562-1
ISBN-10: 0-321-73562-5
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, July 2011
Dedicated to my wife, Agata.
Rod
Dedicated to my wife, Vicki.
Ray
Contents at a Glance
Praise for Learning Cocos2D
If youre looking to create an iPhone or iPad game, Learning Cocos2D should be the first book on your shopping list. Rod and Ray do a phenomenal job of taking you through the entire process from concept to app, clearly explaining both how to do each step as well as why youre dong it.
Jeff LaMarche, Principal, MartianCraft, LLC, and coauthor of Beginning iPhone Development (Apress, 2009)
This book provides an excellent introduction to iOS 2D game development. Beyond that, the book also provides one of the best introductions to Box2D available. I am truly impressed with the detail and depth of Box2D coverage.
Erin Catto, creator of Box2D
Warning: reading this book will make you need to write a game! Learning Cocos2D is a great fast-forward into writing the next hit game for iOSdefinitely a must for the aspiring indie iOS game developer (regardless of experience level)! Thanks, Rod and Ray, for letting me skip the learning curve; youve really saved my bacon!
Eric Hayes, Principle Engineer, Brewmium LLC (and Indie iOS Developer)
Learning Cocos2D is an outstanding read, and I highly recommend it to any iOS developer wanting to get into game development with Cocos2D. This book gave me the knowledge and confidence I needed to write an iOS game without having to be a math and OpenGL whiz.
Kirby Turner, White Peak Software, Inc.
Learning Cocos2D is both an entertaining and informative book; it covers everything you need to know about creating games using Cocos2D.
Fahim Farook, RookSoft (rooksoft.co.nz)
This is the premiere book on Cocos2D! After reading this book you will have a firm grasp of the framework, and you will be able to create a few different types of games. Rod and Ray get you quickly up to speed with the basics in the first group of chapters. The later chapters cover the more advanced features, such as parallax scrolling, CocosDenshion, Box2D, Chipmunk, particle systems, and Apple Game Center. The authors writing style is descriptive, concise, and fun to read. This book is a must have!
Nick Waynik, iOS Developer
Preface
So you want to be a game developer?
Developing games for the iPhone or iPad can be a lot of fun. It is one of the few things we can do to feel like a kid again. Everyone, it seems, has an idea for a game, and what better platform to develop for than the iPhone and iPad?
What stops most people from actually developing a game, though, is that game development covers a wide swath of computer science skillsgraphics, audio, networkingand at times it can seem like you are drinking from a fire hose. When you are first getting started, becoming comfortable with Objective-C can seem like a huge task, especially if you start to look at things like OpenGL ES, OpenAL, and other lower-level APIs for your game.
Writing a game for the iPhone and iPad does not have to be that difficultand it isnt. To help simplify the task of building 2D games, look no further than Cocos2D.
You no longer have to deal with low-level OpenGL programming APIs to make games for the iPhone, and you dont need to be a math or physics expert. Theres a much faster and easier wayuse a free and popular open source game programming framework called Cocos2D. Cocos2D is extremely fun and easy to use, and with it you can skip the low-level details and focus on what makes your game different and special!
This book teaches you how to use Cocos2D to make your own games, taking you step by step through the process of making an actual game thats on the App Store right now! The game you build in this book is called Space Viking and is the story of a kick-ass Viking transported to an alien planet. In the process of making the game, you get hands-on experience with all of the most important elements in Cocos2D and see how everything fits together to make a complete game.
Download the Game!
You can download Space Vikings from the App Store: http://itunes.apple.com/us/app/space-vikings/id400657526mt=8. The game is free, so go ahead and download it, start playing around with it, and see if youre good enough to get all of the achievements!