Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

I grew up playing video games; I remember my Amstrad CPC 128K as one amazing computer that brought to my home several of the games I played in the arcades and I feel lucky for being born at that time. Thanks to that, I've known titles that have passed into history and I've been a witness to the evolution of the video game industry; from the first handheld games to the iPhone 6 and iPad Air 2, passing through the first computers and video consoles to the current next-gen.

This hobby became a passion and I realized very early that I would like to learn to develop video games so I could understand how my preferred titles were built, and this is one of the things I want you to learn through this book. In fact, there are three important things that I would like you to have learned by the end of the book:

  • Love for games: I think that playing any kind of game is one of the most important things in this life. Playing games in a balanced way can make your brain faster; you can learn culture, make friends, laugh, or even live a different life; that's why we should not stop playing.

  • Love for development: When I was a child, I realized that I wanted to be a developer because I wanted to know how to build the things I used to play. Once I became a developer, I realized how many things you can build by yourself with just a keyboard, and I think that making your own game or your own app can be compared to writing a book or recording a film. At the end, it is the same thing; you are creating something from nothing. So that's why I think that there are three things people should do before dying: have a child, plant a tree, and develop a game.

  • Autonomy: I would like you to have learned all the tools you will need to develop whatever is in your mind and to understand how to solve the problems you will find during this process.

From time to time, we hear news about a new game that is breaking the market and turning people crazy in a few weeks, achieving thousands and thousands of downloads, hundreds of thousands of profits from its in-app purchases, or millions of dollars in revenue. What makes this game so addictive? This is the question that most of us mobile game developers ask ourselves and the answer is always similar: a good idea. That's why most of us keep trying daily to think of good ideas to be the pillars of our next game, but this idea never comes to mind.

If you think about it, these addictive games are based on simple things and most of them are based on previous games, but their developers have included some features that make us want to play over and over. That's why it's important to learn which techniques were used to develop successful games from the past and the present.

But to achieve this, it's important to be equipped with the most appropriate tools in order to focus on playability rather than squeezing our brains trying to figure out how to do what we want. Hence, we have developed the games included in this book using Cocos2d because it's based on Objective-C, a language that I'm sure most of you are familiar with.

Cocos2d v3.0

Cocos2d for iOS (http://www.cocos2d-iphone.org) is one of the most powerful and popular frameworks to develop 2D games with. Its popularity is due to its features: it's open source and 2D; has a wide and collaborative community; supports sprites, collisions, scenes, transitions, audio effects, actions, physics, and animations; and has a lot more features.

At the time of writing this book, the current version is Cocos2d v3.0, released a few months ago, so if you have previous experience with this framework, it is possible that you will find some differences and new features.

Pure Objective-C

The syntax has been improved so method names conform to conventions and the code is now better structured. Also, the C libraries have been removed so now we will use just the Core Foundation classes.

ARC

Previously, the new projects created in Cocos2d didn't use Automatic Reference Counting (ARC) by default, but you could enable ARC with a little refactoring process. Now you can forget all these headaches of retaining, releasing, and autoreleasing memory as v3 is ARC-only.

CCDirector

The former [CCDirector sharedDirector].winSize feature has been replaced by the new [CCDirector sharedDirector].viewSize feature.

CCLayer

CCNode has replaced CCLayer. Previously, CCLayer was used mainly to support for touch and accelerometer events but since every node inherits from CCResponder and has this ability, it's no longer needed. Now CCLayer can't be inherited to create new classes to represent scenes; instead of this class, you should use CCScene from now onward.

CCArray

In the first versions of Cocos2d, CCArray was used thanks to the speed it provided. However, this advantage is no longer the case and during Cocos2d v2, a lot of developers were recommending not to use this class to manage arrays. That's why it has been deprecated in this new version and is no longer available.

OALSimpleAudio

CocosDenshion's SimpleAudioEngine, an external class, previously supported sound and audio effects. From now, ObjectAL's OALSimpleAudio class has replaced SimpleAudioEngine. This new class is focused on doing what we need in a simple way, converting complex actions into easy tasks.

CCAction

Almost all the action classes have been renamed to something like CCActionNameAction; despite this, its syntax remains unaltered.

CCActionCallBlock

Good-bye CCCallBlock, hello CCActionCallBlock. Something similar to what happened to CCAction also happens to CCCallBlock; it has been renamed but its syntax remains unaltered.

Sequences

The former CCSequence class is now named CCActionSequence and the most important change in this case is that when passing the array of actions to the sequences, you don't have to pass a nil object as the last element.

Schedule update

In the previous version, you should execute scheduleUpdate in order to schedule the update method to be called every frame. This is not needed anymore; now you just need to implement the update method in the way you want to work.

Enabling touches

Touch handling is now performed by CCNode and therefore by its descendants. To enable it now, you will need to set userInteractionEnabled to TRUE and implement either touchBegan, touchMoved, touchEnded, or touchCancelled.

Accelerometer events

As with touches, to enable accelerometer events handling, you need to set userInteractionEnabled to TRUE and you will also need to add the Core Motion framework.

Physics

Unlike what happened in Cocos2d v2, physics are now based on Chipmunk. Previously, we had both Box2D and Chipmunk to implement physics, but now the only library will be Chipmunk.

What this book covers

In this book, you will find eight chapters, each covering a different genre of video games. The aim of the book is that you learn the features of Cocos2d at the same time as you discover the singularities of several games that have been or are currently successful in the video game industry.

Chapter 1, Sprites, Sounds, and Collisions, covers the first steps of developing with Cocos2d v3.0. This chapter will guide you through how to create a horizontal-scroll game in which you will create sprites from an image. You will also learn how to move sprites across the screen thanks to executing actions, and in case it's needed, how to manage collisions. Also, you will learn how to show score labels and how to play background music and sound effects. The best of all, this game will be available on both iPhone and iPad devices.

Chapter 2, Explosions and UFOs, shows the process of developing a classic shoot 'em up in which the accelerometer takes control of the movement. In this game, you will learn to load and set up particle systems such as explosions and fire and draw primitives (lines, circles, and squares) on a CCNode instance. In addition, you will be introduced to the parallax effect and you will implement it in the game.

Chapter 3, Your First Online Game, teaches you the development of a turn-based game with the particularity that it will allow you to play against another player thanks to the configuration of Game Center in your game. Also, you will learn how to drag, scale, and rotate sprites and how to include labels using bitmap fonts. As this is a turn-based game, you will learn to include timers to control each player's turn.

Chapter 4, Beat All Your Enemies Up, takes you back to the 80s and 90s as you learn to develop one of the classic arcade genres: the beat 'em up. In this chapter, you will learn to create an iPad-only game in which players will move thanks to a track pad you will develop. As this kind of game demands that the characters perform different movements, you will learn to animate the sprites using several image files to optimize sprites' management. You will also will learn how to use sprite sheets.

Chapter 5, Scenes at the Highest Level, covers the development of a brain game in which you will create a tutorial so players can learn the basics of the game. You will learn how to load data from external files to configure the different scenes and how to create transitions between these scenes. In order to keep the user's data, you will learn how to save this information.

Chapter 6, Physics Behavior, introduces you to the physics world but the high-level one because you will learn to take advantage of Chipmunk, a physics engine available in Cocos2d. You will learn to set up and run a Chipmunk simulation and to create and reuse bodies. Collisions are managed by this library too, so you will learn how to use collision filters and listeners to create a sports game.

Chapter 7, Jump and Run, creates a platform game that will handle specific collision logic and manage multiple touches. Also, you will learn how to use multiple sprites to texture varied terrain and you will learn how to add a menu to the game so players can configure some setups.

Chapter 8, Defend the Tower, teaches you how to create a tower defense game and you will learn how to provide Artificial Intelligence to non-playable characters. In addition, you will develop a pathfinder algorithm so these NPCs know which path is best for them. Through this chapter, you will also learn to include notifications and in-app purchases.

What you need for this book

To develop this book's games, you will need the following hardware and software:

  • An Intel-based Mac running Mac OS X 10.8.4 or later

  • The latest Xcode version (5.1.1 at the time of writing this book)

  • To be enrolled in the iOS Developer Program if you want to test the games on a device and to use in-app purchases

  • An iOS device to test games on

You don't need wide development experience in either Objective-C or Cocos2d as these chapters guide you step by step so you understand what is happening. However, you do need to have an intermediate knowledge level of Cocos2d and an understanding of Objective-C.

Who this book is for

If you are a passionate gamer, you like developing, or you're just curious, this book is for you. It has been written to teach 2D game development to app creators and also to teach Objective-C to game developers. In both cases, you will find a common point: games. If you have developed several apps and now you want them to include gamification, or if you have been developing games using other frameworks and you want to take advantage of what Cocos2d offers, this book is for you.

Conventions

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: "A CCScene class is a class that inherits from CCNode and whose main purpose is to contain the behavior of a single scene in the game."

A block of code is set as follows:

    // Create a colored background (Dark Grey)
    CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
    [self addChild:background];

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: "In the project navigator, select the Resources group, right-click and select Add Files to "RunYetiRun"…."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Sometimes, you are going to be challenged by me requesting you to try to solve a particular situation. These challenges will be labeled as 1-star, 2-star, or 3-star challenges depending on the difficulty of the task, but they can be solved with the knowledge acquired throughout the book. They will look like this:

1-star challenge – accurate collision detection

Reader feedback

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 , 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.

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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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 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/7887OS_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 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

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 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

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.