Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Cocos2d Game Development Blueprints
Cocos2d Game Development Blueprints

Cocos2d Game Development Blueprints: Design, develop, and create your own successful iOS games using the Cocos2d game engine

Arrow left icon
Profile Icon Jorge Jordán
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (2 Ratings)
Paperback Jan 2015 440 pages 1st Edition
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Jorge Jordán
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (2 Ratings)
Paperback Jan 2015 440 pages 1st Edition
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Cocos2d Game Development Blueprints

Chapter 2. Explosions and UFOs

One of the most interesting characteristics in iOS devices is the accelerometer, thanks to which you can, among other functionalities, control the movement of the main character of a game without needing to touch the screen. In this chapter, you will learn how to take advantage of this feature and mix it with particles such as explosions and fire to develop a classic shoot 'em up game like 1942, but in this case, the enemies will be UFOs. As you will need some spaceships, you will learn how to create them by extending the CCSprite class and I will also teach you how to implement the parallax effect to create an illusion of depth in a 2D game. To conclude, I will introduce you to geometric primitives such as lines, squares, and circles and how to draw them.

Things you will learn in this chapter include:

  • How to use the accelerometer
  • How to load and set up a particle system
  • How to create sprites that extend CCSprite
  • How to implement the parallax effect...

Handling the accelerometer input

Since the apparition of the first iPhone generation, mobile devices are equipped with a 3-axis accelerometer (x, y, and z) to detect orientation changes and it has modified the course of handheld games. It allows players to interact with games without the need to touch the screen, and it allows developers to create new subgenres of games too.

Throughout this chapter, you will mix this hardware feature with a classic arcades genre to develop the following game: the planet Earth has been attacked by an army of UFOs whose sole purpose is to wipe out all of mankind, but fortunately, a mad scientist equipped with some of his inventions is brave enough to defend us.

To control the movements of Dr. Nicholas Fringe, our mad scientist, you will take advantage of the accelerometer, but first of all you need a clean Xcode project. As you learned in the previous chapter how to create a new project and how to get it ready to start developing, we will skip this step, so...

Implementing the parallax effect in Cocos2d

The parallax effect is a scrolling technique used in 2D games that simulates depth and displacement by moving the foreground layers faster than background layers. This technique was developed initially to be used in traditional animation in the 1930s and was first used in computer games in the early 80s.

This effect consists of making our brain think that we are looking at displacement similar to what happens when we are traveling on a train: we can see the trees near the railway passing fast, but if we look to the houses placed further away, they pass a little slower, and if you look at the mountains in the background, they look almost immobile. If we place our scientist over a ground layer that will displace slowly and above this layer we scroll a clouds layer from the top to the bottom of the screen, our brain will think that Dr. Fringe is flying over the clouds thanks to his amazing backpack-reactor.

CCParallaxNode

As the parallax effect is a...

Particle systems

I always say that video games are comparable to films, differences aside, because they tell stories, have an argument, different scenes, a soundtrack, and special effects. Yes, you read correctly, you can add special effects, or particle systems as they're commonly known in computer games, to your games.

In computer graphics, this technique is commonly used because it simulates several natural and meteorological phenomena such as snow, sun, rain, fire, meteors, and smoke. It also simulates other special effects such as spirals, explosions, fireworks, and other lighting effects by using a large amount of small images. If you think about natural phenomena, like rain for example, it is composed of hundreds of water droplets and so that's what we need to replicate if we want to simulate a particle system in Cocos2d.

You might be thinking that this task must be hard and offers little possibilities, but in this section, you will learn to create and customize your own...

Extending CCSprite

Dr. Nicholas Fringe's enemies are an army of UFOs controlled by very intelligent extraterrestrial beings trying to wipe out all of mankind. That's why we're going to create them as a separate class that will derive from CCSprite, where we will define its evolved behavior.

Some developers prefer to derive this kind of class from CCNode and include a CCSprite instance as it offers more potential, but for the moment we are going to keep it simple and just extend CCSprite.

First of all, let's create the new class:

  1. Right-click on the Classes group in the project navigator and select New File….
  2. Click on iOS | cocos2d v3.x and choose to create the new file from the CCNode class template.
  3. Type CCSprite in the available field and click on Next.
  4. Call the file as UFO and be sure that the Classes folder is selected before clicking on Create.

Then replace the contents of UFO.h with the following block of code:

#import <Foundation/Foundation.h>
#import &quot...

2-star challenge – create explosions

Now that we are able to detect when a UFO is destroyed, it would be more realistic to make them explode, and I think you're ready to develop this by yourself.

Create one explosion whenever a spaceship is destroyed by using the file explosion.png that you will find in the Resources folder.

The solution

You will need to go back a few pages to achieve this challenge because in this case, we're going to create a CCParticleSystemModeRadius particle.

First of all, add explosion.png to the project and then add the following lines inside the method detectCollisions, after [_ufosToRemove addObject:ufo];:

CCParticleExplosion *explosion = [CCParticleExplosion node];
    explosion.texture = [CCTexture textureWithFile:@"explosion.png"];
         explosion.emitterMode = CCParticleSystemModeRadius;
         explosion.startSize = 100.0;
         explosion.startRadius = 20.0;
         explosion.endSize = 1.0;
         explosion.endRadius = 100.0...

Drawing and coloring

Usually, you will be working with sprites, nodes, and other high-level objects, but maybe sometimes you will want to draw some geometric primitives such as lines, circles, or squares. In our case, we're going to use them to represent our scientist's life bar.

Fortunately, Cocos2d allows us to perform these tasks easily by using any of these three options:

  • The draw method: This CCNode class method is the one you should override in your classes derived from CCNode to customize drawing your nodes. It's important here not to call [super draw] or you will face unexpected behavior.
  • The visit method: This CCNode class method gives you more control as it calls the draw method of the node and its children in the order they were added to it, taking into account the specified z-orders.
  • CCDrawNode: This class, derived from CCNode, is the one that gives you full control over the primitives because they are treated as nodes themselves.

The draw method presents a problem...

Handling the accelerometer input


Since the apparition of the first iPhone generation, mobile devices are equipped with a 3-axis accelerometer (x, y, and z) to detect orientation changes and it has modified the course of handheld games. It allows players to interact with games without the need to touch the screen, and it allows developers to create new subgenres of games too.

Throughout this chapter, you will mix this hardware feature with a classic arcades genre to develop the following game: the planet Earth has been attacked by an army of UFOs whose sole purpose is to wipe out all of mankind, but fortunately, a mad scientist equipped with some of his inventions is brave enough to defend us.

To control the movements of Dr. Nicholas Fringe, our mad scientist, you will take advantage of the accelerometer, but first of all you need a clean Xcode project. As you learned in the previous chapter how to create a new project and how to get it ready to start developing, we will skip this step, so...

Implementing the parallax effect in Cocos2d


The parallax effect is a scrolling technique used in 2D games that simulates depth and displacement by moving the foreground layers faster than background layers. This technique was developed initially to be used in traditional animation in the 1930s and was first used in computer games in the early 80s.

This effect consists of making our brain think that we are looking at displacement similar to what happens when we are traveling on a train: we can see the trees near the railway passing fast, but if we look to the houses placed further away, they pass a little slower, and if you look at the mountains in the background, they look almost immobile. If we place our scientist over a ground layer that will displace slowly and above this layer we scroll a clouds layer from the top to the bottom of the screen, our brain will think that Dr. Fringe is flying over the clouds thanks to his amazing backpack-reactor.

CCParallaxNode

As the parallax effect is a...

Left arrow icon Right arrow icon

Description

Whether you are a passionate gamer, like developing, or are just curious about game development, this book is for you. The book has been written to teach 2D game development to app developers and to teach Objective-C to game developers, as learning Cocos2d is the perfect step for both roles.

Who is this book for?

Whether you are a passionate gamer, like developing, or are just curious about game development, this book is for you. The book has been written to teach 2D game development to app developers and to teach Objective-C to game developers, as learning Cocos2d is the perfect step for both roles.

What you will learn

  • Load and control sprites, labels, sounds, and geometrical primitives efficiently to build the core of a game
  • Simulate movement by implementing the parallax effect and running animations
  • Implement turnbased game logic including Game Center
  • Create both iPadonly and universal versions of your games
  • Control your game using touches, an accelerometer, or a virtual game pad
  • Build menus and tutorials and define some artificial intelligence to nonplayed characters

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 30, 2015
Length: 440 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987887
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jan 30, 2015
Length: 440 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987887
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 131.97
Cocos2d Game Development Essentials
$32.99
Learning iPhone Game Development with Cocos2D 3.0
$43.99
Cocos2d Game Development Blueprints
$54.99
Total $ 131.97 Stars icon
Banner background image

Table of Contents

9 Chapters
1. Sprites, Sounds, and Collisions Chevron down icon Chevron up icon
2. Explosions and UFOs Chevron down icon Chevron up icon
3. Your First Online Game Chevron down icon Chevron up icon
4. Beat All Your Enemies Up Chevron down icon Chevron up icon
5. Scenes at the Highest Level Chevron down icon Chevron up icon
6. Physics Behavior Chevron down icon Chevron up icon
7. Jump and Run Chevron down icon Chevron up icon
8. Defend the Tower Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(2 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
John Preston Cua Mar 22, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is the best Cocos2d Book I’ve worked through so far…This book (Cocos2d Game Development Blueprints) guides the reader in developing 7 different types of games:1.) Chapter 2’s “Explosions and UFOs” - A Game that uses accelerometer inputs to manipulate the movement of the player in the game. In this chapter the reader not only learns how to use the accelerometer of the device in one’s game for the actual left right movement of the player but also how to use the technique of parallax scrolling to give the ILLUSION of forward movement of the player. Other important learning points in this chapter are: a.) How to use Particle Systems for Explosion Animation b.) How to use draw Primitive Shapes2.) Chapter 3’s Online Game - The best part of this chapter for me is the working sample code provided that illustrates how to develop a turn-based card game. After working through this chapter you will most likely have a good or at least better idea on how to implement a turn-based online game using Apple’s Game Center. As a side note, most of the Cocos2d books I’ve worked through does not have a chapter on Game Centre and how to develop a turn-based game. So this is one of the more special points of this book.3.) Chapter 4’s “Beat all Your Enemies Up” Game - If you are like me, who has spent a good amount of your childhood time playing BEAT’EM UP games like DOUBLE DRAGON and GOLDEN AXE during the late 80s and early 90s, you may be very glad to know that after working through this chapter you will have the foundation and a STARTING TEMPLATE to work on to create your own BEAT’EM UP GAME. YEHEY!!4.) Chapter 5’s Math Game - This chapter’s GAME TEMPLATE and sample code is very suitable for educational game apps and for creating the SETTINGS screen of any game…very useful but didn’t get5.) Chapter 6’s Snooker Game - This chapter is all about using the CHIPMUNK PHYSICS ENGINE. The sample game in this chapter is a SNOOKER GAME and NOT ANGRY BIRDS…and so don’t expect to get a ANGRY BIRDS GAME TEMPLATE/BLUEPRINT from this chapter, but EXPECT TO LEARN HOW TO USE THE CHIPMUNK PHYSICS ENGINE in your game.6.) Chapter 7’s Jump and Run - This chapter is for all of the MARIO BROTHERS fans that are now Game Developer Aspirants as this chapter shows how to develop a PLATFORM Game like Mario Brothers.7.) Chapter 8’s Defend the Tower - This chapter discusses the development of a Tower Defence Game. For me the most special part of this chapter is its discussion on how to implement the path finding algorithm for the attackers. In other words, this chapter teaches the reader how to make the objects in one’s game move within the game through following a predefined path that you set. This one special knowledge contained in this chapter is something I appreciate a lot as I have not found this knowledge in any of the other Cocos2d books I’ve read except for one that is already totally outdated.In short, I highly recommend this book to anyone who would like to learn to use Cocos2d to create 2D games as this book attempts to give the reader the knowledge on how to create 7 different types of games and it does a fairly good job at that. Also, there are 2 things that set this book apart from most other Cocos2d books (Note: Read my point 2 and 7 above to find out what these are).As of writing this review (March 22, 2015), the contents of this book is still relevant to the current version of Cocos2d-Swift. If you are reading this book quite a while later than March 22, 2015, please ensure that the book’s Cocos2d’s MAJOR version is still applicable to the Cocos2d version you will be using otherwise you may just end up buying a book that is not the most suitable for the tool you will be using. I don’t think booksellers offer a refund for mistakenly buying a outdated book as well.…Below is a copy and paste from the book for people who want to make sure:“In this book, we work with the latest Xcode version (5.1.1 at the time of writing), Cocos2d v3.0.0, and iOS7. “It is also worth noting that the book came out Jan 30 2015.
Amazon Verified review Amazon
Roger Pingleton May 11, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Cocos2D is always evolving, so books tend to go out of date pretty quickly. Never the less, this is a great resource.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.