Book Image

Learning iPhone Game Development with Cocos2D 3.0

By : Kirill Muzykov
Book Image

Learning iPhone Game Development with Cocos2D 3.0

By: Kirill Muzykov

Overview of this book

Table of Contents (19 chapters)
Learning iPhone Game Development with Cocos2D 3.0
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

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.

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.

What you need for this book

To run the code listed in this book, you will need the following:

  • A Mac computer running OS X 10.8.4 (Mountain Lion) or a higher version

  • Xcode 5.1 or a higher version

    Note

    To test the code on your device, you must be enrolled as an iPhone developer on the Apple iOS Developer program. However, you can test all the code in the book on the simulator, except the small part where we use gyroscope to control the game in Chapter 5, Starting the Action, and to test In-App purchases covered in Chapter 12, Standing Out – Integrating Game Center and In-App Purchases.

Sample game art, sound, music, and all the other required assets are included in the book's supporting files, which you can download for free at the Packt website.

Who this book is for

This book is for anyone who wants to start making games. No previous experience in game development or working with previous versions of Cocos2D or any other game engine is required.

You should have at least basic Objective-C knowledge, as all the code in this book as well as the Cocos2D-iPhone framework itself is written in Objective-C. You don't have to be an Objective-C expert, but you should at least know how to create class, add an instance variable and property, call a method, and be familiar with classes such as NSString or NSArray.

Sections

In this book, you will find several headings that appear frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz – heading

These are short multiple-choice questions intended to help you test your own understanding.

Have a go hero – heading

These practical challenges give you ideas for experimenting with what you have learned.

You will also 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, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Name this class Bird and make it a subclass of CCSprite."

A block of code is set as follows:

#import "CCSprite.h"

typedef enum BirdType
{
    BirdTypeBig,
    BirdTypeMedium,
    BirdTypeSmall
} BirdType;

@interface Bird : CCSprite

@property (nonatomic, assign) BirdType birdType;

-(instancetype)initWithBirdType:(BirdType)typeOfBird;

@end

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

@implementation GameScene
{ 
   Hunter *_hunter;
}

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: "After filling out all the fields, click on the Next button."

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:

https://www.packtpub.com/sites/default/files/downloads/0144OS_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/submit-errata, 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.