Since its inception in 2007, the Apple App Store is still going strong with an average of almost 500 apps added on a daily basis. Out of them, almost 80% of the apps are games. Part of the reason for this is the amazing eco-system created by Apple to make the operating system and IDE available free of charge, making it easy to access for the average developer. The other part is Cocos2d, which is still one of the most widely-used free iOS game-development frameworks that makes games and app development very convenient.
With SpriteBuilder integrating Cocos2d into it, this makes it even more awesome, as now it is even easier to develop a simple prototype of a game without any code. Moreover, with minimum effort the same app or game can be run on a varied number of resolutions. Furthermore, we can now also port these games to Android without any need to rewrite the code. Cross-platform iOS game development, which was a dream for so many years, has now finally become a reality.
As Cocos2d itself has been there since the beginning of the first iOS device, the community is strong and helpful. There are also great tools that have been developed by third-party developers that make Cocos2d, an already awesome framework, even more awesome.
This book takes you through the recipes that are required to make any game. The chapters are laid out in a logical manner so that by following each recipe you will get closer to finishing and porting your game by the end of this book.
Hope to see your creations in the App Store soon.
Chapter 1, Sprites and Animations, teaches you how to draw sprites in a scene, add colored sprites, and render 2D shapes using primitives. This chapter will also show you how to animate sprites, move sprites, add different kinds of actions to a sprite, and also look at the parallax effect to create a more dynamic scene.
Chapter 2, Scenes and Menus, will cover how to add scenes to the game, such as a gameplay scene, how to create buttons and labels in the scene, and how to create transitions between the scenes using various transition effects.
Chapter 3, Gestures, Touches, and the Accelerometer, will discuss various methods of user interaction, such as tapping, holding, swiping along with adding accelerometer inputs and gestures to the scene.
Chapter 4, Physics, will provide examples of how to add physics to the scene and make objects respond to physics. This chapter will also look at different body types and body properties, adding sprites to a body, applying force and impulse to a body, detecting collision responses, and building objects, such as a vehicle, by adding physics bodies together.
Chapter 5, Audio, will show you how to edit audio tracks to create music and sound effects, add the background music and audio effects to the game, add a pause and resume button to the game, and add a volume slider to control the volume of audio to the options menu.
Chapter 6, AI and A* Pathfinding, will include ways to add Artificial Intelligence to enemies in the game. This chapter will also look at creating patrolling, chasing, and projectile shooting enemies. It will cover a pathfinding to create more advanced AI logic for enemies using grids.
Chapter 7, Data Storage and Retrieval, will teach you how to save and load game progress on the device using NSUserDefault, create and access files using JSON, Plist, and XML files, for custom storage and retrieval of data.
Chapter 8, Effects, teaches you to add dynamic lighting to your game, manipulate the position and color of the light source dynamically using touch controls, and add 2D shadows to the game created by the light source. You will learn to add effects to the game using the CCEffects class of Cocos2d to create interesting effects in the game.
Chapter 9, Game Tools, provides insights to using industry standard tools, such as TexturePacker, Glyph Designer, PhysicsEditor, Particle Designer, and Sprite Illuminator, to create more visually appealing and performance-optimized games.
Chapter 10, Swift/SpriteBuilder Basics, shows you how to develop games in Apple's latest programming language, Swift. We will see the differences between Objective-C and Swift in this chapter. We will also see how to import Objective-C classes into Swift to reduce the redundancy of code. This chapter will also provide insight into using SpriteBuilder to develop more robust games.
Chapter 11, Porting to Android, will take a look at how to take your existing game developed for iOS and port it to Android. We will learn how to install the Android Xcode plugin, prepare the device for deployment, and finally run the project on an Android device.
To develop games using Cocos2d, all you need is the latest version of OSX El Capitan, SpriteBuilder, and Xcode. All these applications are available in the Apple App Store and are free to download. The recipes in this book are designed for iPads, so iPad 3 onwards can be used for testing purposes. To develop on Android, an Android device is required. It is better to use the Galaxy or Nexus series phones as they are tried and tested.
The structure of the book is designed in such a way that it is easy to access a specific recipe and get the necessary information from it. Each recipe is self-contained and easy to follow. This book is for intermediate to advanced users who have some basic knowledge of how Cocos2d works. Although the basic concepts are covered in this book, they are not explained in depth, so some prior experience is required.
In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
In this book, you will find a number of text styles 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: "We will then create the _sprite1
and _sprite2
variables. In spriteWithImageNames
, we will pass the filename variable that will hold the string of the filename."
A block of code is set as follows:
#import "CCSprite.h" @interface ParallaxSprite :CCSprite{ CGSize _winSize; CGPoint _center; CCSprite *_sprite1, *_sprite2; float _speed; } -(id)initWithFilename:(NSString *)filename Speed:(float)speed; -(void)update:(CCTime)delta; @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:
#import "Hero.h" #import "ParallaxSprite.h" @interface MainScene :CCNode{ CGSizewinSize; Hero* hero; ParallaxSprite* pSprite; }
Any command-line input or output is written as follows:
./install.sh -i
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Next, we will give it a class name. We will select CCSprite as Subclass of and Objective–C as Language."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in 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 at www.packtpub.com/authors.
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.
You can download the example code files for this book 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
We also provide you with 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/Cocos2dCrossPlatformGameDevelopmentCookbookSecondEdition_ColorImages.pdf.
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 could 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.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.