Book Image

Creating Games with cocos2d for iPhone 2

By : Paul Nygard
Book Image

Creating Games with cocos2d for iPhone 2

By: Paul Nygard

Overview of this book

Cocos2d for iPhone is a simple (but powerful) 2D framework that makes it easy to create games for the iPhone. There are thousands of games in the App Store already using cocos2d. Game development has never been this approachable and easy to get started. "Creating Games with cocos2d for iPhone 2" takes you through the entire process of designing and building nine complete games for the iPhone, iPod Touch, or iPad using cocos2d 2.0. The projects start simply and gradually increase in complexity, building on the lessons learned in previous chapters. Good design practices are emphasized throughout. From a simple match game to an endless runner, you will learn how to build a wide variety of game styles. You will learn how to implement animation, actions, create "artificial randomness", use the Box2D physics engine, create tile maps, and even use Bluetooth to play between two devices. "Creating games with cocos2d for iPhone 2" will take your game building skills to the next level.
Table of Contents (16 chapters)
Creating Games with cocos2d for iPhone 2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Cocos2d for iPhone is a robust framework for developing 2D games for any iOS device. It is powerful, flexible, and best of all it is free to use for your own projects. Thousands of apps, including many top selling games, have been written using cocos2d.

Creating Games with cocos2d for iPhone 2 will take you on a tour of nine very different games, guiding you through the designing process and code needed to build each game. All of the games were specifically selected to highlight different approaches to solving the challenges inherent in designing a game.

The games covered in this book are all classic game styles. By focusing on games you probably already know, you can see how everything works "under the hood" with cocos2d.

What this book covers

Chapter 1, Thanks for the Memory Game, covers the design and building of a memory tile game. It covers basic concepts such as grid layouts, using cocos2d actions, and using CocosDenshion for sound effects.

Chapter 2, Match 3 and Recursive Methods, walks through the design and building of a match-3 game. This chapter covers two different approaches to checking for matches, as well as an extensive section on predictive matching and how to generate artificial randomness.

Chapter 3, Thumping Moles for Fun, provides the basic concepts of how to design a mole thumping game. This game uses Z-ordering to "trick the eye", and uses cocos2d actions extensively to give a very polished game with very little coding needed.

Chapter 4, Give a Snake a Snack..., follows the design and building of a snake game. Some of the topics covered in this chapter include overriding methods, making sprites follow each other, and implementing increasing difficulty levels.

Chapter 5, Brick Breaking Balls with Box2D, covers the building of a brick-breaking game using the Box2D physics engine. In this chapter, you will find a basic primer on how to use Box2D, using plists to store level data, and implementing power-ups.

Chapter 6, Cycles of Light, takes us to an iPad only multiplayer game. This game allows two players to compete head-to-head on the same iPad, or by using GameKit's Bluetooth connectivity to play against each other on two iPads. This chapter also walks through how we can use a single pixel to draw almost everything in the game.

Chapter 7, Playing Pool, Old School, revisits the Box2D physics engine to build a top-down pool game. The focus on this chapter is to implement a simple "rules engine" into the game, as well as how to easily build multiple control methods into the same game.

Chapter 8, Shoot, Scroll, Shoot Again, walks through the building of a top-down scrolling shooter. This chapter walks you through how to use readily available outside tools and resources, including Sneaky Joystick and the Tiled tile map editor. It also covers two different forms of enemy AI, including A* Pathfinding.

Chapter 9, Running and Running and Running..., brings us to the most ambitious game of all, the endless runner. The primary topics covered are how to create random terrain that characters can walk on, parallax scrolling backgrounds, and implementing a lot of different types of enemies.

What you need for this book

The book and code bundle contain the complete source code you will need to run all nine games. You will only need a few items to run the games:

  • An Intel-based Macintosh running OS X Lion (or later)

  • Xcode version 4.5 (or higher)

  • To run any games on a real device (iPhone, iPad, or iPod Touch), you will need to be enrolled in the Apple's iOS Developer program. You will be able to run the games in the iOS Simulator without enrolling, but the tilt controls in Chapter 8, Shoot, Scroll, Shoot Again and the Bluetooth multiplayer mode in Chapter 6, Cycles of Light will only work on a real iOS device.

Who this book is for

This book is written for people who have basic experience with cocos2d, but want some guidance on how to approach real-world design issues. Although the book does revisit some basic concepts, we hit the ground running, so having a basic understanding of cocos2d is recommended. At least some knowledge of Objective-C is also strongly recommended.

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: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

-(void) makeTransition:(ccTime)dt
{
  [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:1.0
        scene:[MTMenuScene scene] withColor:ccWHITE]];
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

-(void) makeTransition:(ccTime)dt
{
  [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:1.0
        scene:[MTMenuScene scene] withColor:ccWHITE]];
} 

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: "clicking the Next button moves you to the next screen".

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 through the subject of your message.

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.

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.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output.

You can download this file from http://www.packtpub.com/sites/default/files/downloads/9007OS_ColoredImages.pdf

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 to our website, or added to any list of existing errata, under the Errata section of that title.

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.