Learning iPhone Game Development with Cocos2D 3 · PDF fileLearning iPhone Game Development...

15
Learning iPhone Game Development with Cocos2D 3.0 Kirill Muzykov Chapter No. 1 "All About Cocos2D"

Transcript of Learning iPhone Game Development with Cocos2D 3 · PDF fileLearning iPhone Game Development...

Learning iPhone Game Development with Cocos2D 3.0

Kirill Muzykov

Chapter No. 1 "All About Cocos2D"

In this package, you will find: A Biography of the author of the book

A preview chapter from the book, Chapter NO.1 "All About Cocos2D"

A synopsis of the book’s 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 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.

For More Information: www.packtpub.com/learning-iphone-game-development-with-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 from the 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 them I 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 Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Learning iPhone 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 Information: www.packtpub.com/learning-iphone-game-development-with-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 https://www.packtpub.com/sites/default/files/downloads/0144OS_Chapter_12.pdf.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

1All 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 wri en in Python and was fi 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 func ons.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

All About Cocos2D

[ 8 ]

Game enginesLong before the mobile-games era or even PC-gaming era, each game was created for a specifi c hardware. This means that if you wanted to create a game, you had to spend some me learning low-level details of hardware specifi ca on and take into account all of its

characteris cs, constraints, and some mes, issues.

Then, when the game was completed and you wanted to create a diff erent game, there was very li le you could reuse from the previous games you created. We're not even speaking about por ng your exis ng game to another pla orm. In this case, you just had to rewrite it almost from scratch.

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

So what is a game engine?A game engine is a so ware framework that adds a level of abstrac on between hardware, video drivers, sound drivers, and your code. Typical game engines provide the following func onali es:

Rendering: This is the main func on of the majority of game engines. This func on helps you actually draw something on the screen without the need to write any low-level code. In addi on, a game engine sets up a lot of features for you, such as ini alizing 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 addi on to sta c 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 fl 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 fi nger swipe, touching to ac vate 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 Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Chapter 1

[ 9 ]

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

Some game engines provide addi onal func onali es such as physics engine, collision detec on, scrip ng, ar fi cial intelligence, networking and mul player support, and localiza on support. However, many great games were created only using the three func onali es men oned 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 specifi ca on. So, you have no excuses to not start crea ng 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 func ons.

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 crea ng 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. A er releasing your fi rst game, you will get invaluable experience, and later, if you decide to dive deeper into low-level details, you will fi nd it much easier a er 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 s ll leave all the rou ne work to Cocos2D.

For More Information: www.packtpub.com/learning-iphone-game-development-with-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 easyYou can start crea ng your fi 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 crea ng a fun and exci ng experience for your players. You don't have to master Cocos2D to create your fi rst game that might reach the top of the AppStore.

Cocos2D is freeYes, Cocos2D is completely free for commercial use. You will never have to pay anything for it, and there are a bunch of extensions, u li es, 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 un l you start to earn some real money, and I'm sure you're planning to do that.

I know it is temp ng to buy the new, shiny game engine, because you think that if it costs money, it is be er. 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 s ll get it for free! Even more, big companies are not only using Cocos2D, but also contribu ng to it!

Cocos2D is popularCocos2D 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 implemen ng the required func onality.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Chapter 1

[ 11 ]

Cocos2D is open sourceYou 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 fi x; 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 wri en, 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 addi on to all the preceding goodies, you will get the following too:

Physics simula on with the Chipmunk physics engine Visual tools for building interfaces and levels Por ng to Android with only a few steps Lots of ready-to-use controls A lot of other features

Por ng to Android has become possible due to eff orts 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 defi nitely stands out from the rest. High chances that a er 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 anima ons, 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 fi rst you need to learn pure Cocos2D and a er that it will be very easy to start using SpriteBuilder and make your life even easier.Both SpriteBuilder and por ng to Android are not covered in this book.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

All About Cocos2D

[ 12 ]

Games created with Cocos2DI'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 signifi cant part of the book.

BADLANDBADLAND is a very atmospheric side-scroller ac on 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 Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Chapter 1

[ 13 ]

Feed Me Oil 2Feed Me Oil 2 is a great mind-bending puzzle game, where you need to deliver oil to the target zone using many diff erent 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 Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

All About Cocos2D

[ 14 ]

Lep's World 2Lep's World 2 is a really cool and fun pla ormer game with more than 60 million downloads. The game reached the number one place in several countries and s ll 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 gamesThe 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 ra ngs, 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 submi ed games.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Chapter 1

[ 15 ]

SummaryIn this chapter, we've learned some general informa on about game engines and Cocos2D in par cular. Don't worry if this part seems too abstract; it serves several very important purposes.

First, you should understand what benefi ts you get from using game engines, that is, a higher level of abstrac on 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 s ll 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 prac cal approach. We will download and install Cocos2D and will create and run our fi rst Cocos2D project.

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book

Where to buy this book You can buy Learning iPhone Game Development with Cocos2D 3.0 from the Packt Publishing website: http://www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-0/book.

Free shipping to the US, UK, Europe and selected Asian countries. For more in formation, p lease read our shipping policy.

Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.

www.PacktPub.com

For More Information: www.packtpub.com/learning-iphone-game-development-with-cocos2d-3-

0/book