Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Overview of this book

Cocos2d is the world’s leading game development framework for developing iOS games. With the introduction of Swift and Spritebuilder, it has become easier than ever to develop the games of your dreams without much effort. With Cocos2d, you can also deploy the game on Android, thereby maximizing profit and reducing development and porting costs. The book starts off with a detailed look at how to implement sprites and animations into your game to make it livelier. You will then learn to add scenes to the game such as the gameplay scene and options scene and create menus and buttons in these scenes, as well as creating transitions between them. From there on, you will get an understanding of how to program user interactions such as tapping, holding, and swiping. You’ll then add accelerometer inputs and physics to the scene, and make objects respond back to the inputs. A game is practically incomplete without audio being added, so this will be covered next. The next section will include ways to add Artificial Intelligence to enemies in the game, allowing them to patrol, chase, and shoot in a projectile manner. You will then learn to use NSUserDefault to save and load game progress, and create and access files using JSON, Plist, and XML files for custom storage and retrieval of data. Then you will learn to add dynamic lighting to your game and will use industry-wide tools such as Texture Packer, Glyph Designer, Physics Editor, Particle Designer, and Sprite Illuminator to create more visually appealing and performance-optimized games. Towards the end of the book, we dive into Apple’s latest programming language—Swift, highlighting the major differences between Objective C and Swift. The book culminates with taking your existing game developed for iOS and porting it to Android, showing you how to install the Android Xcode plugin as well.
Table of Contents (19 chapters)
Cocos2d Cross-Platform Game Development Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Downloading and installing Cocos2d


To create and run a Cocos2d project, you have to install SpriteBuilder and Xcode. In this section, we will briefly cover how to do so.

Getting ready

To download and install Cocos2d, go to http://cocos2d.spritebuilder.com/download.

SpriteBuilder is the official installer of Cocos2d now. To install, click on the Cocos2d-SpriteBuilder installer link. This will open the Mac App Store Preview page.

If you would like to download an older version of Cocos2d, you can go through the links in the archive section of the page and download it from there as well.

Click on the View in Mac App Store link and then click on Launch Application when prompted.

Once the App Store opens, click on Install to start the installation process. You will need to sign in to an account to download the file.

Once installed, it should be in your applications folder. Open up Launchpad and click on the application to open it.

Once the application starts, you will be asked to join the SpriteBuilder mailing list. Add your e-mail address and click on Continue or select Sign Up Later.

We will go through how to create a small project using SpriteBuilder later in the book. For now, we are ready to create a new project to work with. Navigate to File | New Project and select a location to create the project folder.

You can also select the primary language for coding. As we will use Objective-C, make sure that it is selected at the bottom.

Once you have selected the location for the project, in the Save As box at the top, give the project a name. Remember the name and the location of the project folder as we will need it to open the project in Xcode.

Next, we need to open the project in Xcode. We will first understand how to code using Xcode as it is essential to make more complex games. In later chapters, we will consider how to use SpriteBuilder to simplify our game development process.

You can close the SpriteBuilder project as it is not required anymore.

If you don't have Xcode, you can download it from the Mac App Store.

The installation process is very similar to any other app.

How to do it…

Perform the following steps:

  1. Once you install Xcode, go the to the project folder in the directory you stored the project in and double-click on projectname.xcodeproj. Here, I named the project Sprite, so I will double-click on Sprite.xcodeproj:

  2. Once it is open, you should see the interface as follows:

How it works…

Click on the play button in the upper-left corner to run the project.

This will run and show the default project running on the simulator. Congrats! You have Cocos2d running successfully.