Creating Games with cocos2d for iPhone 2 - Paul Nygard - E-Book

Creating Games with cocos2d for iPhone 2 E-Book

Paul Nygard

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 502

Veröffentlichungsjahr: 2012

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Creating Games with cocos2d for iPhone 2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Thanks for the Memory Game
The project is...
Let's build a menu
Where is the scene?
Building the playfield
We need sprites
Building a sprite sheet
On to the playfield
Creating the playfield header
Creating the playfield layer
The flow of the game
A stack of tiles
The memory tile class
Loading tiles
Drawing tiles
Adding interactivity
Checking for matches
Scoring and excitement
Animating the score
Adding lives and game over
Bringing it all together
It's quiet...too quiet
Summary
2. Match 3 and Recursive Methods
The project is…
Basic gem interaction
The MAGem header
The MAGem class
Generating gems
Building the playfield
Checking for matches
Collecting touches
Moving gems
Checking moves
Removing gems
The update method
Predictive logic
Artificial randomness
Summary
3. Thumping Moles for Fun
The project is…
Design approach
Designing the spawn
Portrait mode
Custom TTF fonts
Defining a molehill
Building the mole
Making a molehill
Drawing the ground
Mole spawning
Special moles
Moving moles
The animation cache
Combining actions and animation
Simultaneous actions
Deleting moles
Touching moles
Tying it together
Scoring the mole
Summary
4. Give a Snake a Snack…
The project is…
Design approach
Building a better snake
Anatomy of a snake segment
Dissecting the snake
Building the head
Building the body segments
Moving the snake
Turning the snake
Death of a snake
Building the environment
Outer walls
Inner walls
Building snake food
Collisions and eating
Levels and difficulties
The main loop
Level-up checking
Dead mice
But…how do we control the snake?
Summary
5. Brick Breaking Balls with Box2D
The project is…
Box2D – a primer
Box2D – what is it?
Basic parts of Box2D
On to the game!
World building
On the edge
Having a ball
Setting everything in motion
Collision handling
Losing your ball
Destruction
Paddling around
Paddle fixture
Touching the paddle
Storing player data
Displaying player data
Building bricks
Loading a plist
Picking a pattern
Breaking bricks, for real
Power-ups, good and bad
Picking up power-ups
Paddle deformation
Restoring the paddle
Multiball
Losing lives with multiball
Summary
6. Cycles of Light
The game is…
Design review
Let's build a bike
CLBike header
CLBike implementation
Bike rotation
Turning the bike
Building walls
Boundary walls
Bike walls
Bike integration
Bike movement
Control buttons
Touching buttons
Flashing with blocks
Finishing the buttons
Building the background grid
Drawing the grid
The second grid
Moving grids
The glScissor
The playfield
Generating the bikes
Collision handling
Making it move
Crashing bikes
Bluetooth multiplayer
Peer Picker
Session callbacks
Sending messages
Receiving data
Upgrading our bikes
Why send moves?
Summary
7. Playing Pool, Old School
The game is…
Overall design
Building the table
The Box2D world
Building the rails
Building pockets
Creating the cue stick
Loading the rules
Rules.plist
Rack 'em up
Building the rack
Player HUD
Displaying messages
Collision handling
Building the control base
One-touch control
Two-touch control
The rules engine
Putting balls back
Checking the table
The playfield init method
Summary
8. Shoot, Scroll, Shoot Again
The game is…
Design review
Tiled – a primer
Drawing the ground
Logic layers
Spawn layer
Understanding TMX format
Creating an HD map
Implementing the tilemap
Adding our hero
Focus on the hero
Controlling the hero with SneakyJoystick
Tilt controls
Interpreting the controls
Building the HUD
Scene construction
Tile helper methods
Tile self-identification
Smarter hero walking
Time for bullets
TDBullet class
Building the enemy
Adding the enemies
Collision handling
Everybody gets hit
Game over, man
Smarter enemies
Code not covered here
Summary
9. Running and Running and Running...
The game is...
Design review
Building the ground
ERTile class
Adding gap tiles
Scrolling the tiles
Parallax background
Our hero
Animation loading
Updating the hero
Touch controls
Shooting bullets
Enemies everywhere
Collision handling
Getting shot with particles
Death of hero
Summary
Index

Creating Games with cocos2d for iPhone 2

Creating Games with cocos2d for iPhone 2

Copyright © 2012 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2012

Production Reference: 1171212

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-84951-900-7

www.packtpub.com

Cover Image by Abhishek Pandey (<[email protected]>)

Credits

Author

Paul Nygard

Reviewers

Dave Hersey

Marcio Valenzuela P

Acquisition Editor

Usha Iyer

Lead Technical Editor

Llewellyn F. Rozario

Technical Editors

Sharvari Baet

Komal Chheda

Kirti Pujari

Brandt D'Mello

Project Coordinator

Abhishek Kori

Proofreaders

Bob Phillips

Bernadette Watkins

Indexer

Monica Ajmera Mehta

Production Coordinator

Shantanu Zagade

Cover Work

Shantanu Zagade

About the Author

Paul Nygard has been a computer and technology enthusiast since he was introduced to his first computer at the age of six. He has spent most of his professional life building reporting and analytical systems for a large company. After teaching himself Objective-C in 2010, he has focused most of his attention on iOS programming. Paul created a personal development label, Troll Cave Games (http://www.trollcavegames.com), to release his mobile gaming efforts. Paul also works as a consultant and developer-for-hire for visionaries in need of a coder. In his leisure time, Paul is passionate about games, books, and music.

I would like to thank my family for their support and for allowing me to hide in my troll cave and pursue my passions. This book is for you!

I would also like to thank Ricardo Quesada and Steve Oldmeadow, for all their hard work on cocos2d and cocosDenshion. They have helped to mold a new generation of developers by giving us all a wonderful world to play in. And to the cocos2d community: I have never seen such a large group of developers so eager to help each other along. Thanks for the support, advice, and humor over 
the years.

About the Reviewers

Dave Hersey has over 35 years of experience in Apple software development, dating back to the Apple II personal computer in 1977. After a successful career of more than 6 years at Apple Computer, Inc., Dave started Paracoders, Inc. in 2000 focusing on custom Mac OS X-based application and driver development. In 2008, Dave's business expanded into iOS (iPhone) mobile applications, followed by Android applications soon after. Some bigger-named clients include Paramount Home Entertainment, Lionsgate Entertainment, Seagate, Creative Labs, and Kraft. Most recently, Dave's business expansion has included additional mobile and server-side platforms as well as support services. This has led to rebranding his company as "Torchlight Apps" (http://www.torchlightapps.com).

Dave is also a moderator and active participant on the cocos2d-iphone.org forums under the username clarus when he's not busy with his wife raising three children, three dogs, three parakeets, four ducks, and seven ducklings at last count.

Marcio Valenzuela P is a biochemist who has studied programming as a hobby for over 12 years. He is perseverant, auto-didactic, and is always looking into the latest technologies. Marcio started by picking up ASP back in the early 1990s as Chief Web Developer for a consulting company that developed web applications for private companies. He also delved into PHP applications with a MySQL database backend. Then in 2008 he started his path down iOS and has had experience developing applications and games for the platform. His experience is mostly in business applications where there exists a cloud-based web service to interact with and more recently in games created in cocos2d.

Marcio is cofounder of Activasolutions.com and currently runs a small iOS project called Santiapps.com, which programs for companies wishing to enter the iOS platform. Marcio is a forum moderator at RayWenderlich.com.

I would like to acknowledge the time I have taken from raising my son to dedicate to this book. I just hope someday Santiago follows in the programming tradition as it fosters critical skills such as problem solving and innovation which is something we share.

www.PacktPub.com

Support files, eBooks, discount offers, and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read, and search across Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

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 <[email protected]>, 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 <[email protected]> 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 <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Thanks for the Memory Game

As children, we learn many useful skills by playing games. We learn coordination, strategy, and memory skills. These are all skills we take with us throughout our lives. The perfect place to start is a traditional childhood game.

In this chapter, we cover the following topics:

Scenes versus layersSprites and sprite sheetsLoading sequential filesRandom playfield generationTouch handlersUsing actionsBasic matching strategiesScoringTracking livesGame over conditionsSimpleSoundEngine

The project is...

We will begin with classic memory game. Not just any memory game – the memory game that is the source of joy and frustration of children everywhere. In case you've never been exposed to this game (really?), the gameplay is simple. The playing field is a set of tiles with pretty pictures on the front, and a generic back image. You turn over two tiles to see if you made a match. If not, you turn them back over. Pick another pair of tiles and see if they match. Repeat this till all the tiles are matched. Let's take a look at the finished game:

Our memory game needs to be flexible enough to allow different skill levels in the game. We will create different skill levels by varying the number of memory tiles on the board. If there are four tiles (two each of two designs), that's pretty easy. Creating a 4 x 5 grid of tiles is a lot more challenging (20 tiles, 10 designs). We will build a single project that will handle these variations dynamically.

Our game will be a little different from the traditional version in two ways: it is single player only, and we will add a way to lose the game, to make it more exciting. We'll go into detail on that aspect later.

Note

We will detail several foundational design approaches in this chapter that will be used throughout the book. To avoid repetitive code, later chapters will omit some of the boilerplate detail that we cover here.

We need sprites

Before we proceed with building the playing field, we need some graphics to use in our game. Our design calls for square images for the tiles, and one image to use for the common back of the tiles. Because we want them to be able to scale to different sizes (for our different skill levels), we need images large enough to look good at the simplest skill level, which is a two by two grid. Unless your goal is the "chunky pixel" look, you never want to scale images up. Based on the screen size, we want our tiles to be 150 points wide and 150 points high. Since we want to use better graphics on iPhone (and iPod Touch) Retina Display devices, our -hd version of the graphics will need to be 300 pixels by 300 pixels.

Tip

Points are the easiest way to use cocos2d effectively. On an older iOS Device, one point equals one pixel on the screen. On Retina Display devices, one point equals two pixels, which occupy the same physical space on the screen as the one pixel on the non-Retina screens. From a practical perspective, this means that once you provide the -hd graphics, cocos2d will treat your Retina and non-Retina layouts identically, with no extra layout code. You can do things in pixels if you really want to, but we wouldn't recommend making a habit of it.

For this game, we will be using a variety of photos. There was some amount of manipulation needed to arrive at the proper aspect ratio and size. This is a great place to make use of Automator that is part of Mac OS X. There is an Automator script in the source code for this chapter inside a folder called Helpers. When you run it, it will prompt for a folder of images. Once selected, it will create a folder on your desktop called ch1_tiles, and it will contain sequentially numbered images (that is tile1.png, tile2.png, and so on), with each image being exactly 300 pixels by 300 pixels.

Two other images will be needed to build the game: backButton.png will be used for navigation and tileback.png will be used as the image for the back of the tiles when they are face down.

Building a sprite sheet

Sprite sheets are one of the foundations of any effective cocos2d game. Compiling all of your sprites into sprite sheets lets cocos2d load fewer files as well as giving you the ability to use a batch node. We won't delve into the "under the hood" details of the CCSpriteBatchNode class here except at a high level. When you load a sprite sheet into a batch node, it acts as a "parent" for all the sprites on it. When you use a batch node, the calls to draw the sprites on the screen are batched together, which gives a performance boost. This batch drawing allows the system to draw 100 sprites with about the same efficiency (and speed) as drawing one sprite. The bottom line is batch nodes allow you to draw a lot more on-screen without slowing your game down.

There are two files needed for a sprite sheet: the texture (image file) and the plist file. We don't even want to think about attempting to hand-build the sprite sheet. Fortunately, there are a number of very good tools that were built for this. The most established sprite sheet tools in the cocos2d community are TexturePacker (http://www.texturepacker.com) and Zwoptex (http://zwopple.com/zwoptex), although there are a number of newer apps that are also available. Which tool you use is a matter of personal preference. Regardless of the tool, you will need to create both the standard and -hd versions of the images. (Most current tools have built-in options to aid in this process.)

No matter which tool is used, the desired result is four files: memorysheet.png, memorysheet.plist, memorysheet-hd.png, and memorysheet-hd.plist. The -hd files include the 300 x 300 images for the iPhone Retina Display, and the others include the 150 x 150 pixel images for non-Retina iPhone Displays. We also include the backButton.png and tileback.png files in appropriate sizing in both sprite sheets as well. Let's take a look at the final sprite sheet we will use for this game:

Scoring and excitement

The game works well with the development we have covered, but there are a few places where we can add a little more visual flair and some excitement. Players love having a game that keeps score. They also love animation. There is excitement to be had by having the ability to lose. Let's give the player what they want.

We build the score and lives displays using CCLabelTTF labels, with the variables playerScore and livesRemaining as their label contents. These are declared as variables of the layer, so we can easily update them. When we start animating tiles, it will be useful to know where the score and lives displays are on screen.

There are two main approaches to adding text to the screen: CCLabelTTF and CCLabelBMFont. Both have their uses, which we will briefly outline here. CCLabelTTF uses a standard TTF font file. The way it draws the text on the screen is not very efficient and can cause performance issues in some uses. The other approach, CCLabelBMFont, uses a bitmap (image file) of the font and internally uses a batch node to render the text. This means it is highly efficient at drawing, with very little performance concern. Other than the use of a TTF file versus an image file, the way you code for them is very similar. One potential issue with a BMFont file is that you must have the entire font in a single bitmap. If you are using a large font size, this often causes you to need to leave out some characters that may be needed to support international keyboards. A TTF file does not have this problem. Also, it is common with the CCLabelBMFont approach to have multiple versions of the font if you want to use different font sizes. In this book, we will use CCLabelTTF labels throughout because we do not have any performance (frame rate) concerns with any of these projects.

If we were to have performance issues, we would certainly switch to using CCLabelBMFont instead of CCLabelTTF. We leave it as an exercise for the reader to convert these projects to use the CCLabelBMFont class. (For creation of the bitmaps, an excellent resource is Glyph Designer, available at http://glyphdesigner.71squared.com.)

Filename:MTPlayfieldLayer.m

-(CGPoint) scorePosition { return ccp(size.width - 10 - tileSize.width/2, (size.height/4) * 3); } -(CGPoint) livesPosition { return ccp(size.width - 10 - tileSize.width/2, size.height/4); }

Rather than hardcoding the values in multiple places, it is a preferred approach to create helper methods such as scorePosition and livesPosition, which return a CGPoint reference of where those elements are onscreen. Here we see the calculations, which place the score and lives near the left edge of the screen, with the score three quarters of the way up the screen, and the lives one quarter of the way up the screen.

The creation of simple labels is very basic, using the positioning we saw above. To see how the score and lives are created, please consult the accompanying code bundle for this book.

Now we will need a way to score and animate the tiles when the player makes a successful match. When a match is scored, we will fly the tiles to the score, and then have them shrink into the score position until they disappear. Let's see how that works:

Filename:MTPlayfieldLayer.m