You are on page 1of 15

Learning i Phone Game Development

with Cocos2D 3.0











Kirill Muzykov









Chapter No. 1
"Al l About Cocos2D"
In this package, you will find:
A Biography of the author of the book
A preview chapter fromthe book, Chapter NO.1 "All About Cocos2D"
A synopsis of the books content
Information on where to buy this book









About the Author
Kirill Muzykov has been passionate about programming since the age of six, when
he wrote his first line of code in BASIC. After graduating fromuniversity, he worked
for a couple of years in a software development company, but then decided he wanted
to be in charge and founded a new company with two of his colleagues.
It took several years to grow the company froma small teamconsisting of cofounders
to one of the leading companies in the region. However, after almost six years and dozens
of completed projects for clients, he tried game development and fell in love with it.
A few years ago, he made a big turn in his career and started working as an independent
game developer.
In his free time, he loves to play games and watch Simpsons and Futurama. You can
find his blog and forumfor this book at www. ki r i l l muzykov. com.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
I would like to thank my wife, Svetlana, who always believed in me. With
her support, it was much easier to write this book. Also, she created all the
art for this book and saved you fromthe nightmares that you would definitely
have had if you would have seen my drawings.
I would like to thank both our families for their constant support and
patience. Thanks to all my friends; without themI wouldn't become the
person I am.
Also, thanks to the reviewers of this book. The issues you found, as well
as your ideas and suggestions, definitely made this book better.
Finally, I want to thank all the people who contributed to Cocos2D. Without
them, writing this book simply wouldn't be possible. Special thanks to
Ricardo Quesada for creating Cocos2D for iPhone and to Lars Birkemose
for leading it forward!




For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Learning i Phone Game Development
with Cocos2D 3.0
Cocos2D Swift is a popular and robust framework used to develop 2D games and
applications. It is fast, easy to use, open source, and has a big and friendly community.
It is free, and you can use it for commercial purposes with no restrictions.
Cocos2D can be used to create games and applications for iOS, OS X, and even
Android (via Apportable). There are thousands of games in the AppStore created
with this Cocos2D framework, including many bestsellers.
Cocos2D Swift is written in Objective-C and was previously named Cocos2D for iPhone.
Renaming the project simply shows that Cocos2D developers are looking into the future
and will continue improving the project to support all the latest Apple's innovations.
At the time of writing this book, you can only use Objective-C to create games using
Cocos2D Swift, but in future, both languages will be supported. This book covers the
Cocos2D API and Cocos2D concepts that will remain unchanged, independent of the
language you use to write your code.
There are several ways to learn a new topic, but I think the best way to learn game
development is by actually making games. In this book, we're going to take a practical
approach and complete many common tasks that arise when creating games. We will
work on rendering and animating sprites and text, handling user input and controlling the
game, implementing game logic, adding sound effects and music, building user interface,
using physics, and so on.
At the end of this book, you will know all the basics and will have some practical
experience to start creating games.
What This Book Covers
Chapter 1, All About Cocos2D, provides basic information about game engines,
additional information about Cocos2D, as well as examples of great games created
with Cocos2D.
Chapter 2, Hello Cocos2D, guides you through the installation process and reviews
the contents of the Cocos2D distribution package and demo projects that come with it.
Chapter 3, Cocos2D Under the Hood, describes the architecture of the framework
and its main classes. In the second part of this chapter, we will review several Cocos2D
configuration options.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Chapter 4, Rendering Sprites, begins to unveil the process of game creation. In this
chapter, we will add a game scene, background image, player, and enemy characters.
We will review some of the main properties of Cocos2D nodes and will make them
move, rotate, flip, and so on.
Chapter 5, Starting the Action, covers the process of controlling the game using states,
handling touches, or using a gyroscope to get player input. At the end of this chapter,
we will have a skeleton of a playable game.
Chapter 6, Rendering Text, shows you how to display score, lives, earned points,
and winning and losing labels. In this chapter, we will use both True Type and Bitmap
font-based labels of Cocos2D and will discuss benefits and performance considerations.
Chapter 7, Animations and Particle Systems, demonstrates the use of different
animation types and shows how to use particle systems to get really cool effects such
as explosion and fire.
Chapter 8, Adding Sound Effects and Music, shows how to easily add sound effects
and music, switch between music tracks, and adjust audio properties.
Chapter 9, User Interface and Navigation, concentrates on creating a convenient user
interface using Cocos2D controls such as buttons, the scroll view, table view, and so
on. In this chapter, we will see how to create scenes that exist in most games, such as
the menu scene, about scene, and so on, and how to navigate between them.
Chapter 10, Physics, shows how to use the physics engine in your game. In this chapter,
we will create a playable level using the physics engine; we will review how to create
physics objects, adjust their properties, detect and filter collisions, use joints, and so on.
Chapter 11, Working with Tile Maps, explains tile maps and shows the complete
process of creating and using a tile map in the game.
Chapter 12, Standing Out Integrating Game Center and In-App Purchases,
covers integrating Game Center and adding In-App purchases to the game. This is
a downloadable chapter and you can download this chapter from
ht t ps: / / www. packt pub. com/ si t es/ def aul t / f i l es/ downl oads/ 0144OS_
Chapt er _12. pdf .




For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
1
All About Cocos2D
Before diving deep into the fun world of game development, we will spend
some time (but only a little) understanding what Cocos2D is, how it can help
you develop games, and why you should use it.
In this chapter, we're going to review the following points:
What a game engine is and why you should use it
Why you should choose Cocos2D as a game engine
A list of games created with Cocos2D
There are several versions of Cocos2D. The original Cocos2D is wrien
in Python and was rst released in 2008. There is also a C++ version
called cocos2d-x, HTML5 version called cocos2d-HTML5, and several
other versions.
In this book, we're going to review the currently most popular version
of Cocos2D, that is, Cocos2D Swi (previously named Cocos2D-iPhone).
However, for brevity, I will just call it Cocos2D.
If in future you will want to learn some other version, it will be quite
easy, because all versions share the same architecture, main concepts,
and a majority of funcons.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
All About Cocos2D
[ 8 ]
Game engines
Long before the mobile-games era or even PC-gaming era, each game was created for a
specic hardware. This means that if you wanted to create a game, you had to spend some
me learning low-level details of hardware specicaon and take into account all of its
characteriscs, constraints, and somemes, issues.
Then, when the game was completed and you wanted to create a dierent game, there was
very lile you could reuse from the previous games you created. We're not even speaking
about porng your exisng game to another plaorm. In this case, you just had to rewrite it
almost from scratch.
But hey! We're very lucky to live in a dierent me. A game developer's life is much easier
these days. In our me, we have a lot of dierent game engines at our disposal; these make
our lives much easier.
So what is a game engine?
A game engine is a soware framework that adds a level of abstracon between
hardware, video drivers, sound drivers, and your code. Typical game engines provide
the following funconalies:
Rendering: This is the main funcon of the majority of game engines. This funcon
helps you actually draw something on the screen without the need to write any
low-level code. In addion, a game engine sets up a lot of features for you, such as
inializing graphics and loading textures. For example, using a modern game engine,
you can draw a player character on screen with 2-3 lines of code compared to
30-50 lines of code if you're using OpenGL ES 2.0 directly. In addion to stac
drawing, game engines provide the ability to animate, scale, rotate, and do many
other useful things just by changing an object property.
User input: There might be a few great games I don't know about, where you don't
have to do anything and don't control the game ow in any way. You just watch what
happens on the screen and enjoy (if you know of such games, let me know!). All the
recent popular games take some kind of user input (cu ng the rope with a nger
swipe, touching to acvate jetpack, and so on). A game engine lets you get that user
input in a convenient manner and respond to it in the game.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Chapter 1
[ 9 ]
Sound: Games with sound are so much beer! Adding the right sound eects and
music can turn a mediocre game into a real gem. Again, game engines come to the
rescue and let you play sound eects and background music with only a few lines
of code.
Some game engines provide addional funconalies such as physics engine, collision
detecon, scripng, arcial intelligence, networking and mulplayer support, and
localizaon support. However, many great games were created only using the three
funconalies menoned earlier.
So what is this all about?
First, it is nice to know that you're lucky to live in a me when you can take a game engine
and concentrate on making great games instead of spending me on learning hardware
specicaon. So, you have no excuses to not start creang games right now!
Second, Cocos2D is a game engine, so it is a good idea to get a basic understanding of a
game engine, its purpose, and its main funcons.
Third, many developers including myself love to reinvent the wheel. If you're just beginning
game development and considering whether you should invest your me in learning a game
engine and using it, or learning low-level features such as OpenGL and possibly creang your
own game engine with blackjack, I strongly advise you to start with a game engine such
as Cocos2D.
You will learn a lot of game development concepts, and more importantly, you will be able
to actually make games. Aer releasing your rst game, you will get invaluable experience,
and later, if you decide to dive deeper into low-level details, you will nd it much easier aer
working with a game engine.
Also, it is nice to know that Cocos2D is based on OpenGL ES 2.0 and won't constrain you in
any way. You will be able to write low-level code where you think it is required, but sll leave
all the roune work to Cocos2D.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
All About Cocos2D
[ 10 ]
Why Cocos2D?
Now that we have discussed why game engines are good for you, let's have a look why you
should use Cocos2D.
Cocos2D is easy
You can start creang your rst game in no me. A few chapters later, you will see this
yourself. Cocos2D has a very beginner-friendly learning curve, and although there is a lot
you can learn about Cocos2D, you can actually start making games and learn as you go.
This is very important, as making games is not about learning advanced tools; it is about
creang a fun and excing experience for your players. You don't have to master Cocos2D
to create your rst game that might reach the top of the AppStore.
Cocos2D is free
Yes, Cocos2D is completely free for commercial use. You will never have to pay anything
for it, and there are a bunch of extensions, ulies, tools, and frameworks that work with
Cocos2D. Of course, there are other free game engines, but some of them just don't reach
the required quality level, and some of them are free only unl you start to earn some real
money, and I'm sure you're planning to do that.
I know it is tempng to buy the new, shiny game engine, because you think that if it costs
money, it is beer. Well, it is not always true. It is hard to believe, but Cocos2D's quality
can be compared to top-level commercial products, and you sll get it for free! Even more,
big companies are not only using Cocos2D, but also contribung to it!
Cocos2D is popular
Cocos2D has a huge and very responsive community. You will almost never be le alone,
struggling to solve some complicated tasks. A task is either already solved and maybe
even integrated in Cocos2D, or there is a third-party framework or class implemenng
the required funconality.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Chapter 1
[ 11 ]
Cocos2D is open source
You can have any opinion about open source projects, but I can assure that you will be
very glad to have access to full source code when things go wrong. When you just start
learning Cocos2D, you will mostly make errors that are easy to spot and x; we've all
done them. However, later, when you start doing some advanced stu, having access to
the under-the-hood internals of game engine is just great!
Regarding the source code, it is well wrien, well-structured, and commented. You can
learn a lot just from reading it. In fact, I would suggest every beginner game developer
read it at some point.
Want more?
In addion to all the preceding goodies, you will get the following too:
Physics simulaon with the Chipmunk physics engine
Visual tools for building interfaces and levels
Porng to Android with only a few steps
Lots of ready-to-use controls
A lot of other features
Porng to Android has become possible due to eorts of the Apportable
company (www.apportable.com), which is a sponsor of Cocos2D project.
They are also providing a great free tool called SpriteBuilder, which denitely
stands out from the rest. High chances that aer some me this will be the
only tool you need to develop games for Cocos2D. With SpriteBuilder, you can
design your scenes using GUI editor, create animaons, edit physics shapes,
and many more.
However, using SpriteBuilder doesn't eliminate the need to write the code and
understand how Cocos2D works. This is why, I strongly believe that rst you
need to learn pure Cocos2D and aer that it will be very easy to start using
SpriteBuilder and make your life even easier.
Both SpriteBuilder and porng to Android are not covered in this book.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
All About Cocos2D
[ 12 ]
Games created with Cocos2D
I'm sure you are wondering if there are any great games created with Cocos2D. The answer
is that there are plenty! Let's have look at some of them right now.
This is only the p of the iceberg. We physically cannot list all the
great games made using Cocos2D, as it will take up a signicant part
of the book.
BADLAND
BADLAND is a very atmospheric side-scroller acon game with great gameplay and graphics.
BADLAND received a lot of awards (including an award from Apple) and reached the number
one place in more than 80 countries.
The game makes good use of physics and has an amazing design. BADLAND was developed
by a two-man game studio called Frogmind. A screenshot of the game is as follows:



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Chapter 1
[ 13 ]
Feed Me Oil 2
Feed Me Oil 2 is a great mind-bending puzzle game, where you need to deliver oil to the
target zone using many dierent devices. The game reached the top of the AppStore and
was featured in many countries. Its predecessor, Feed Me Oil, was also a great success!
The game was developed by an independent game developer, Alexander Ilin. A screenshot of
the game is as follows:



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
All About Cocos2D
[ 14 ]
Lep's World 2
Lep's World 2 is a really cool and fun plaormer game with more than 60 million downloads.
The game reached the number one place in several countries and sll is very popular. I think
you won't be surprised to know that it was created using Cocos2D. A screenshot of the game
is as follows:
Other games
The listed games here are just a small set of all the games created using Cocos2D. The games
listed earlier were picked because of their quality, high rangs, and to be honest, because I
like them. There are thousands of games created with Cocos2D.
If you don't believe me and want to see a huge list of Cocos2D games, please visit http://
www.cocos2d-iphone.org/games. However, note that this is also not a full list of games,
as it contains only the manually submied games.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Chapter 1
[ 15 ]
Summary
In this chapter, we've learned some general informaon about game engines and
Cocos2D in parcular. Don't worry if this part seems too abstract; it serves several very
important purposes.
First, you should understand what benets you get from using game engines, that is, a higher
level of abstracon and faster development at the cost of less control. Don't worry; this cost
is minimal with Cocos2D and in fact, is close to zero as Cocos2D is an open source project.
Also, if you were sll deciding whether Cocos2D is the right way to go, this chapter should
have dispelled all doubts. You've seen some examples of great and successful games
developed using Cocos2D. I believe they will inspire you to create your own great and
successful game.
In the next chapter, we're going to use a more praccal approach. We will download and
install Cocos2D and will create and run our rst Cocos2D project.



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book
Where to buy this book
You can buy Learning iPhone Game Development with Cocos2D 3.0 fromthe Packt
Publishing website: ht t p: / / www. packt pub. com/ l ear ni ng- i phone- game-
devel opment - wi t h- cocos2d- 3- 0/ book.
Free shipping to the US, UK, Europe and selected Asian countries. For more information, please
read our shipping policy.
Alternatively, you can buy the book fromAmazon, BN.com, Computer Manuals and
most internet book retailers.



















www.PacktPub.com



For More Informati on:
www.packtpub.com/l earni ng-i phone-game-devel opment-wi th-cocos2d-3-
0/book

You might also like