Learning iPhone Game Development with Cocos2D 3.0

By Kirill Muzykov
  • Instant online access to over 8,000+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. All About Cocos2D

About this book

Cocos2D-iPhone (Cocos2D-Swift) is a robust yet simple-touse 2D game framework for iPhone. If you are just starting with game programming, Cocos2D will enable you to make your first game in no time. Even if you are a seasoned game developer, you will still be able to benefit from what Cocos2D offers.

This book will set you on the fast-track to game development with Cocos2D for iPhone. You will start by reviewing the basic classes and hierarchy, dive deep into animations and game flow, and finish by creating a complete game with UI using some advanced techniques.

You will begin the book with an empty Cocos2D project and build on it until you end up with a complete game. The book will teach you how to work on game logic, handling user input, controlling the character, and playing audio. Later, in the final part of the book, we'll have a working game on our hands, and we'll see how we can improve the game further

Publication date:
June 2014
Publisher
Packt
Pages
434
ISBN
9781782160144

 

Chapter 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

    Note

    There are several versions of Cocos2D. The original Cocos2D is written in Python and was first 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 Swift (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 functions.

 

Game engines


Long before the mobile-games era or even PC-gaming era, each game was created for a specific hardware. This means that if you wanted to create a game, you had to spend some time learning low-level details of hardware specification and take into account all of its characteristics, constraints, and sometimes, issues.

Then, when the game was completed and you wanted to create a different game, there was very little you could reuse from the previous games you created. We're not even speaking about porting your existing game to another platform. In this case, you just had to rewrite it almost from scratch.

But hey! We're very lucky to live in a different time. A game developer's life is much easier these days. In our time, we have a lot of different game engines at our disposal; these make our lives much easier.

So what is a game engine?

A game engine is a software framework that adds a level of abstraction between hardware, video drivers, sound drivers, and your code. Typical game engines provide the following functionalities:

  • Rendering: This is the main function of the majority of game engines. This function helps you actually draw something on the screen without the need to write any low-level code. In addition, a game engine sets up a lot of features for you, such as initializing 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 addition to static 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 flow 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 (cutting the rope with a finger swipe, touching to activate jetpack, and so on). A game engine lets you get that user input in a convenient manner and respond to it in the game.

  • Sound: Games with sound are so much better! Adding the right sound effects and music can turn a mediocre game into a real gem. Again, game engines come to the rescue and let you play sound effects and background music with only a few lines of code.

Some game engines provide additional functionalities such as physics engine, collision detection, scripting, artificial intelligence, networking and multiplayer support, and localization support. However, many great games were created only using the three functionalities mentioned earlier.

So what is this all about?

First, it is nice to know that you're lucky to live in a time when you can take a game engine and concentrate on making great games instead of spending time on learning hardware specification. So, you have no excuses to not start creating 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 functions.

Third, many developers including myself love to reinvent the wheel. If you're just beginning game development and considering whether you should invest your time in learning a game engine and using it, or learning low-level features such as OpenGL and possibly creating 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. After releasing your first game, you will get invaluable experience, and later, if you decide to dive deeper into low-level details, you will find it much easier after 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 still leave all the routine work to Cocos2D.

 

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 creating your first game in no time. 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 creating a fun and exciting experience for your players. You don't have to master Cocos2D to create your first 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, utilities, 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 until you start to earn some real money, and I'm sure you're planning to do that.

I know it is tempting to buy the new, shiny game engine, because you think that if it costs money, it is better. 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 still get it for free! Even more, big companies are not only using Cocos2D, but also contributing to it!

Cocos2D is popular

Cocos2D has a huge and very responsive community. You will almost never be left 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 implementing the required functionality.

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 fix; we've all done them. However, later, when you start doing some advanced stuff, having access to the under-the-hood internals of game engine is just great!

Regarding the source code, it is well written, 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 addition to all the preceding goodies, you will get the following too:

  • Physics simulation with the Chipmunk physics engine

  • Visual tools for building interfaces and levels

  • Porting to Android with only a few steps

  • Lots of ready-to-use controls

  • A lot of other features

    Note

    Porting to Android has become possible due to efforts 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 definitely stands out from the rest. High chances that after some time this will be the only tool you need to develop games for Cocos2D. With SpriteBuilder, you can design your scenes using GUI editor, create animations, 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 first you need to learn pure Cocos2D and after that it will be very easy to start using SpriteBuilder and make your life even easier.

    Both SpriteBuilder and porting to Android are not covered in this book.

 

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.

Note

This is only the tip of the iceberg. We physically cannot list all the great games made using Cocos2D, as it will take up a significant part of the book.

BADLAND

BADLAND is a very atmospheric side-scroller action 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:

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 different 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:

Lep's World 2

Lep's World 2 is a really cool and fun platformer game with more than 60 million downloads. The game reached the number one place in several countries and still 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 ratings, 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 submitted games.

 

Summary


In this chapter, we've learned some general information about game engines and Cocos2D in particular. Don't worry if this part seems too abstract; it serves several very important purposes.

First, you should understand what benefits you get from using game engines, that is, a higher level of abstraction 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 still 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 practical approach. We will download and install Cocos2D and will create and run our first Cocos2D project.

About the Author

  • Kirill Muzykov

    Kirill Muzykov has been passionate about programming since the age of six, when he wrote his first line of code in BASIC. After graduating from university, 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 from a small team consisting 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 forum for this book at www.kirillmuzykov.com.

    Browse publications by this author
Book Title
Unlock this full book with a FREE 10-day trial
Start Free Trial