Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Cocos2d Game Development Essentials
Cocos2d Game Development Essentials

Cocos2d Game Development Essentials: For new users - a quickstart guide to bringing your mobile game ideas to life with Cocos2D , Second Edition

eBook
€8.99 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.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 Essentials

Chapter 2. Nodes, Sprites, and Scenes

In the last chapter, you went through the introduction of Cocos2d template. You had a look at your first nodes, and sprites, and learned how to move between scenes. In this chapter, you will get into more detail on how these elements work and what can be done with them. You will also create your first mini game to begin with, to see how the pieces all come together.

In this chapter, you will learn:

  • How to build blocks of Cocos2d nodes
  • How to display images on the screen
  • How to layout your scene
  • How to change between different scenes

The building blocks, nodes

In Cocos2d, everything you see on screen is a subclass of the CCNode class. Even the scenes your game has are subclasses of the CCNode class.

There are some important subclasses that will be used in almost every game. Therefore, it is important to know them. These are as follows:

  • CCSprite: This represents an image that can be animated
  • CCNodeColor: This is a plain colored node
  • CCLabelTTF: This is a node that renders text in any TrueType font
  • CCButton: This is an interactive node that can have an action attached
  • CCLayoutBox: This is a node that lays out other nodes in a vertical or horizontal layout

Tip

The CCLayoutBox node replaces the CCMenu class, which was in the previous versions of Cocos2d.

The reason every class inherits from the CCNode class is that there are some important properties that all visible elements on-screen need. These are as follows:

  • contentSize: This is the size of the element in the unit that is specified in the contentSizeType property
  • position: This...

Putting it into practice

It's now time to start working on your first game. You will put all the information you have learned so far into practice, and create a very simple game using nodes and sprites. The game will be a simple one where you try to catch water in a bucket. The game will get faster and faster as it goes on. It's probably not going to make you the next App Store millionaire, but you have to start somewhere right?

Here is what our game will look like when it is finished:

Putting it into practice

Start by making a new Cocos2d project in Xcode just like you did in the last chapter. You can also delete the HelloWorldScene classes as you won't be using them.

Open up the IntroScene.m file, and delete all the code from the init method, except the required initialization and return statement. Also, you can delete the onSpinningClicked method. You should end up with an implementation, as shown in the following code:

@implementation IntroScene

// ----------------------------------------------...

The next step

Congratulations on making your first game in Cocos2d. There are many possible extensions you could make:

  1. The game currently only drops one drop at a time. It was set up to use an array for drops; however, you should be able to easily drop more than one drop at once.
  2. Currently the drop falls behind the water bucket; this is not what the designer intended. Modify the game so the drop falls into the bucket, as shown in the following figure:
    The next step

    Tip

    Don't forget that you can always build and run your app for Android using Apportable.

The Cocos2d update loop

In our game, the update method was seen for the first time. This will most likely be used in every game, so it will now be covered in more detail.

There are two types of update methods:

  • update:(CCTime)delta: This update method has a dynamic time step. It is called directly before the frame is rendered. Cocos2d attempts to render your game at 60 frames per second.

    Note

    If your game consumes too much processing time, then the game's frame rate will decrease. The update method will then be called less-than 60 times per second.

    The delta parameter shows the time since the last update call in milliseconds.

  • fixedUpdate:(CCTime)delta: This update method is guaranteed to be called at a specified interval. It is recommended to use this when you require property changes on physics-based objects. The integrated physics engine operates on this update method.

These update methods are available inside every CCNode. The update method is called on each node by the CCDirector class...

Scenes

Scenes in Cocos2d are used extensively to break the game into manageable components. While it would be possible to create your entire game in one scene, it is certainly not advisable. The scene is the root node of your node hierarchy. Each scene is a subclass of the CCNode class. Only one scene can be active at any time, and changing the active scene is managed by the CCDirector class .

Scene life cycle

Cocos2d provides multiple events that are called at certain points in the life cycle of your scene. You can override these events in any CCNode subclass, and not just the CCScene class. The following events are available:

  • init: This method is called when a scene is initialized in code. If the scene was created in SpriteBuilder, this method is not called. This method is normally where you build your scene by adding child nodes.
  • didLoadFromCCB: This method is called when your scene is created in SpriteBuilder. It is called when the complete scene is loaded and all the code connections have...

Summary

You now have a basic knowledge of Cocos2d and have created your first game! You know about CCNode and the scene graph. You learned the common CCNode subclasses, including CCSprite for 2D images, CCButton for buttons, and CCLabelTTF for text. You also learned how to organize your game into different scenes using CCScene and, how to transition between these scenes using the CCDirector-shared instance. You also learned how to run code on every frame by using the update methods provided in CCNode.

In the next chapter, you will discover how you can avoid all the repetitive initialization code by using the graphical editor: SpriteBuilder.

Left arrow icon Right arrow icon

Description

If you are a game developer with experience in Objective-C and are interested in creating games for iOS or Android, this is the book for you. It will help you to quickly get started with Cocos2D and guide you through the process of creating a game, giving you the essential skills and knowledge you need to do so.

Who is this book for?

If you are a game developer with experience in Objective-C and are interested in creating games for iOS or Android, this is the book for you. It will help you to quickly get started with Cocos2D and guide you through the process of creating a game, giving you the essential skills and knowledge you need to do so.

What you will learn

  • Use sprites and nodes to create scenes to form the foundation of your game
  • Discover how SpriteBuilder and its graphical tools can be used to develop your game and increase its complexity
  • Design professional, impressive, and funfilled animations
  • Create an impressive user interface for an engaging gaming experience
  • Explore and use physics engines for attractive gameplay and use them to add a professional finish to your game
  • Add physics to your game for a more realistic experience
  • Accept user input through a variety of methods including touch and accelerometer

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 23, 2015
Length: 136 pages
Edition : 2nd
Language : English
ISBN-13 : 9781784390327
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 23, 2015
Length: 136 pages
Edition : 2nd
Language : English
ISBN-13 : 9781784390327
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 91.97
Learning Cocos2d-JS Game Development
€24.99
Cocos2d Game Development Blueprints
€41.99
Cocos2d Game Development Essentials
€24.99
Total 91.97 Stars icon
Banner background image

Table of Contents

7 Chapters
1. Getting Started with Cocos2d Chevron down icon Chevron up icon
2. Nodes, Sprites, and Scenes Chevron down icon Chevron up icon
3. SpriteBuilder Chevron down icon Chevron up icon
4. Animation with SpriteBuilder Chevron down icon Chevron up icon
5. User Interaction and Interface Chevron down icon Chevron up icon
6. Physics Engines 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 Half star icon 4.5
(2 Ratings)
5 star 50%
4 star 50%
3 star 0%
2 star 0%
1 star 0%
Cole Apr 02, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is perfect for those looking to get into Cocos2D game development on IOS and Android devices. Before going any further into this review, I would just like to point out first that instructions through out this textbook relies on the user working in a Mac OS environment.Personally, I had no experience with Cocos2D when I first delved into this textbook, and I lacked any Apple hardware to follow along with the tutorials contained with in. Although this was my case, I was still able to learn and follow along thanks to their images of what the game would look like after given code examples. I now feel more confident with the fact that I can work in an Apple environment, and development full games utilizing Cocos2D.Following each tutorial will not leave the reader in the dark with resources to use. In the beginning of the textbook, a link is provided to download images used as examples throughout the book. Perhaps you want to be more creative? A segment of the book will cover an application called "Sprite Builder" downloaded from the Apple App Store, and how easy it is to have it work with Cocos2D.This was a well put together textbook, with steps to help ease new users into using Cocos2D without being tossed into the deep end of Game Development. At no point did the book ever feel overwhelming. If you would like to start developing game for IOS and Android, I would certainly recommend the book to you.
Amazon Verified review Amazon
Christopher Haupt Apr 19, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Our Indie Studio builds its own projects as well as pays the bills with some consulting/for hire work. I’m a believer in in-expensive (time, cost) ways to test ideas, using rapid prototyping via paper or digitally. It is a big win if you can test a game idea, mechanic, technique, or interesting tool before getting too far committed on a project.So, while I'm not a beginner by any stretch, I had this prototyping mindset going while reading a review copy of "Cocos2D Game Development Essentials”. The book's subtitle is "Bring your mobile game ideas to life with Cocos2D”, and that got me thinking about "idea testing" using Cocos2D again. Recently I've been more Unity3D focused for prototyping, but interesting work in the Cocos2D universe has lead me to want to dive in again.This book is a VERY brief dive into Cocos2D and the new cool SpriteBuilder. I like that it walks you through some of the really core framework concepts of nodes, sprites, and scenes. Most of the book is devoted to using SpriteBuilder to quickly assemble a game. Note that the book uses Objective-C (not Swift, though you can find other good resources for Apple's language of the future). While iOS is the default target, the book does mention Android and Apportable (which makes sense, give the company's involvement with sponsoring the project).I'm happy with this book as a quick weekend "tutorial" to guide me mostly through getting a handle on what I might be able to do with SpriteBuilder. The book is definitely not a thorough and complete guide to the tools or the framework, so don't expect that. It may pique your interest enough to look elsewhere (I suggest tutorials from places like http://www.raywenderlich.com/).I'd give it 5 stars if it didn't end so abruptly. You get a taste of using physics (via Chipmonk) and then boom, you are done. No sound, networking, or general publishing. It feels cut off and I don't get a good set of pointers of what to read/do next.
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.