Book Image

Building Android Games with Cocos2d-x

By : Raydelto Hernandez
Book Image

Building Android Games with Cocos2d-x

By: Raydelto Hernandez

Overview of this book

Table of Contents (16 chapters)
Building Android Games with Cocos2d-x
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Cocos2d-x is the most used open source game framework. It has official support from Microsoft for their mobile and desktop platform, and its small core runs faster than other frameworks, allowing it to run on low-end Android devices with outstanding performance. It is currently maintained by an active open source development community led by the author of the original Cocos2d for iPhone and Chukong Technologies.

This introductory book will guide you through the steps for creating a simple two-dimensional game for Android from scratch. During this journey, you will learn the fundamentals of the Cocos2d-x C++ multiplatform game framework and how to handle sprites, add physics to your games, play sounds, display text, generate realistic explosions using particle systems, and add native Android functionalities using Java Native Interface (JNI).

What this book covers

Chapter 1, Setting Up Your Development Environment, guides you step by step for configuring Cocos2d-x and all its prerequisites.

Chapter 2, Graphics, covers handling backgrounds, sprites, animating them, and boosting their performance using sprite sheets.

Chapter 3, Understanding Game Physics, demonstrates the basics of the new Cocos2d-x physics engine based on Chipmunk, which was introduced in Cocos2d-x version 3.0. We will create physics-based bodies, add gravity to them, and detect collisions.

Chapter 4, User Input, is where we add interaction to our game, allowing it to interact with the user with the help of touch listeners and the accelerometer.

Chapter 5, Handling Text and Fonts, proves that handling text is crucial for game development. Irrespective of the complexity of your game, the odds are that you will display information, sometimes with foreign character sets. This chapter shows you how to use simple true-type fonts and more stylized bitmap fonts to make your game look more professional.

Chapter 6, Audio, shows that part of the emotion of playing a game comes from the music and sound effects. Within this chapter, you learn how to add background music and sound effects to your game using the CocosDenshion audio engine, which has been present since the original Cocos2d iPhone game engine. This chapter also covers how to play media using the new audio engine and highlights the major differences between them.

Chapter 7, Creating Particle Systems, illustrates the creation of realistic explosions, fire, snow, rain using the built-in particle systems engine. This chapter shows you how to create your own particle system when you require a customized effect, using the most popular tools.

Chapter 8, Adding Native Java Code, helps you when you need to add native code for creating and invoking Android-specific behavior from within your Cocos2d-x game activity. We do this using the Java Native Interface (JNI) mechanism, available on the Android platform.

What you need for this book

In order to follow this book's narrative and be able to reproduce all the steps, you will need a PC with Windows 7 or higher, any Linux distribution or a Mac running the operating system, and OS X 10.10 Yosemite. Most of the tools that we'll use throughout the book are free to download. We've explained how to download and install them.

Who this book is for

This book was written for people with little or no experience in game programming, and with notions of the C++ programming language, who are willing to create their first Android game in a very comprehensive way.

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 folder names, filenames, file extensions, pathnames, dummy URLs, and user input are shown as follows: "In order to add accelerometer support to our game, we are first going to add the following method declaration to our HelloWorldScene.h header file."

A block of code is set as follows:

void HelloWorld::movePlayerByTouch(Touch* touch, Event* event)
{
  Vec2 touchLocation = touch->getLocation();
  if(_sprPlayer->getBoundingBox().containsPoint(touchLocation)){
    movePlayerIfPossible(touchLocation.x);
  }
}

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

   Size screenSize = glview->getFrameSize();
   Size designSize(768, 1280);
   std::vector<std::string> searchPaths;   
   searchPaths.push_back("sounds");

Any command-line input or output is written as follows:

cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR

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 on 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 , and mention the book title via 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.

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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

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.