Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By : Nathan Burba
Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By: Nathan Burba

Overview of this book

Cocos2d for iPhone is a robust but simple-to-use 2D game framework for iPhone. It is easy to use, fast, flexible, free, and Appstore approved. More than 2500 AppStore games already use it, including many best-seller games. Do you want to take your cocos2d game development skills to the next level and become more professional in cocos2d game design? Cocos2d for iPhone 1 Game Development Cookbook will help you reach that next level. You will find over 100 recipes here that explain everything from the drawing of a single sprite to AI pathfinding and advanced networking. Full working examples are emphasized. Starting with the first chapter, Graphics, you will be taken through every major topic of game development. You will find both simple and complex recipes in the book. Each recipe is either a solution to a common problem (playing video files, accelerometer steering) or a cool advanced technique (3D rendering, textured polygons). This cookbook will have you creating professional quality iOS games quickly with its breadth of working example code.
Table of Contents (15 chapters)
Cocos2d for iPhone 1 Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Cocos2d for iPhone is a robust but simple to use 2D game framework for iPhone. It is fast, flexible, free, and App Store approved. More than 2500 App Store games already use it, including many best-selling games. Do you want to take your Cocos2d game development skills to the next level and become more professional in Cocos2d game design?

Cocos2d for iPhone 1 Game Development Cookbook will help you reach that next level. You will find over 90 recipes here that explain everything from the drawing of a single sprite to AI pathfinding and advanced networking. Full working examples are emphasized.

Starting with the first chapter, Graphics, you will be taken through every major topic of game development. You will find both simple and complex recipes in the book.

Each recipe is either a solution to a common problem (playing video files, accelerometer steering) or a cool advanced technique (3D rendering, textured polygons).

This cookbook will have you creating professional quality iOS games quickly with its breadth of working example code.

What this book covers

Chapter 1, Graphics, covers a wide array of topics. It starts by taking a look at the basic uses of sprites. From there it provides examples for 2D and 3D primitive drawing, video playing, particle effects, ease actions, texture filled polygons, palette swapping, lighting, parallaxing, and more.

Chapter 2, User Input, provides examples of different styles of input typically used in iOS game development. This includes tapping, holding, dragging, buttons, directional pad, analog stick, accelerometer, pinch zooming, and gestures.

Chapter 3, Files and Data, discusses techniques for persisting data. These include PLIST files, JSON files, XML files, NSUserDefaults, archive objects, SQLite, and Core Data.

Chapter 4, Physics, covers a large number of uses of the Box2D physics engine. Examples include debug drawing, collision response, different shapes, dragging, physical properties, impulses, forces, asynchronous body destruction, joints, vehicles, character movement, bullets, ropes, and finally creating a top-down isometric game engine.

Chapter 5, Scenes and Menus, provides examples of user interface implementations. It starts with examples involving scene management then moves to typical UI elements like labels, menus, alert dialogs, and UIKit wrapping. From there it moves into more advanced techniques like draggable menu windows, scrolling menus, sliding menus, loading screens, and a minimap.

Chapter 6, Audio, covers a wide range of audio topics which vary in difficulty. These include sounds, music, audio properties, fading audio, in-game examples, positional audio, metering music and dialogue, recording, streaming, playing iPod music, creating a MIDI synthesizer, speech recognition, and text to speech.

Chapter 7, AI and Logic, discusses techniques for adding intelligent AI actors into your games. These include processing waypoints, firing projectiles at moving targets, line of sight, and flocking behavior using Boids. The pathfinding problem is tackled in four separate recipes: A* pathfinding in a grid, a Box2D world, a TMX tilemap, and a side-scroller. Finally, the chapter discusses adding Lua scripting support, dynamically loading scripts, and using Lua for dialogue trees.

Chapter 8, Tips, Tools, and Ports, provides example uses of commonly-used tools including the Cocos2d-iPhone testbed, Zwoptex, Tiled, JSONWorldBuilder, and CocosBuilder. It also discusses porting a Cocos2d project to C++ using Cocos2d-X and using Cocos3d to develop a 3D iOS game. Finally, it discusses the process of releasing your app on the Apple App Store.

What you need for this book

This book includes projects that contain fully functioning example code. You'll need the following to run the example code:

  • An Intel-based Macintosh running Snow Leopard (OSX 10.6 or later).

  • XCode (4.0 or later recommended).

  • You must be enrolled as an iPhone developer in order to test the example projects on a device. They can be run in the iPhone Simulator without the aforementioned enrollment.

Who this book is for

If you want to elevate your basic Cocos2d project to the next level, then this is the book for you. Some understanding of Objective-C and Cocos2d is recommended. People with some programming experience may also find this book useful.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "In this recipe we will cover drawing sprites using CCSprite, spritesheets, CCSpriteFrameCache, and CCSpriteBatchNode."

A block of code is set as follows:

@implementation Ch1_DrawingSprites
-(CCLayer*) runRecipe {
/*** Draw a sprite using CCSprite ***/
CCSprite *tree1 = [CCSprite spriteWithFile:@"tree.png"];

Any command-line input or output is written as follows:

afconvert -f caff -d ima4 mysound.wav

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Right-click your project under Groups & Files."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

For more information on Cocos2d or for any questions, you can log on to www.Cocos2dCookbook.com.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.