Introduction to Cocos2d

41
Thomas V. Frauenhofer Beret Applications LLC 360iDev Austin, November, 2010 Introduction to Cocos2d: Learning from Examples Monday, November 8, 2010

description

The talk I gave at 360iDev Austin in November, 2010 on Cocos2D development for iOS.

Transcript of Introduction to Cocos2d

Page 1: Introduction to Cocos2d

Thomas V. FrauenhoferBeret Applications LLC

360iDev Austin, November, 2010

Introduction to Cocos2d:Learning from Examples

Monday, November 8, 2010

Page 2: Introduction to Cocos2d

Who am I?

• Software developer for 28+ years

• IBM’er by day, mobile developer at night

• 10 years in Palm OS mobile development

• (May it rest in peace)

Monday, November 8, 2010

Page 3: Introduction to Cocos2d

Chromatic Cosmos

• Cocos2d-Based Tower defense game released last August

• FREE on iTunes this week!

http://beret.com

Monday, November 8, 2010

Page 4: Introduction to Cocos2d

Overview

• Motivation - Why Cocos2D?

• Definition of simple project (BlockGame)

• Building the project

• We won’t finish, but you’ll get all the code

Monday, November 8, 2010

Page 5: Introduction to Cocos2d

Why Cocos2D?

Monday, November 8, 2010

Page 6: Introduction to Cocos2d

Why Cocos2D?

• Simple 2D Gaming SDK

• Built upon the OpenGL API’s

• Rich Objective-C API

• Used in thousands of apps

• Very active developer community

Monday, November 8, 2010

Page 7: Introduction to Cocos2d

Even more Why Cocos2D

• Integrates with some very useful libraries

• Physics (Chimpunk, Box2D)

• Sound (CocosDension)

• Game boards (cocoslive)

• And others...

Monday, November 8, 2010

Page 8: Introduction to Cocos2d

Things to Consider• Cocos2D is an open-source project

• Read the licenses carefully

• Cocos2D has changed A LOT over the past year

• Support for newer iOS versions/features

• Changes in names and abstractions used

• Adopting a new version may be a significant amount of work

Monday, November 8, 2010

Page 9: Introduction to Cocos2d

A brief overview of Cocos2D

Monday, November 8, 2010

Page 10: Introduction to Cocos2d

High-level Cocos2d• The Director (CCDirector) is the boss

• Manages the window and the scenes

• Manages the interactions with the underlying OpenGL environment

• A game has one or more scenes (CCScene)

• Think of a scene in the thematic sense

Monday, November 8, 2010

Page 11: Introduction to Cocos2d

Sample Scene workflow

source: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:basic_concepts

Monday, November 8, 2010

Page 12: Introduction to Cocos2d

It all started with a (CC)Node...• CCNode is the base class of (almost) all Cocos2D

classes

• Many attributes are defined

• Position

• Size

• z-order

• etc...

Monday, November 8, 2010

Page 13: Introduction to Cocos2d

• The origin is the lower left-hand corner of the screen

• The position of an object is the center of the object

image source: http://www.anima-entertainment.de

A quick note on coordinates

Monday, November 8, 2010

Page 14: Introduction to Cocos2d

Layers in Cocos2d

source: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:basic_concepts

Monday, November 8, 2010

Page 15: Introduction to Cocos2d

Layers

• A layer (CCLayer) is where a user interacts with Cocos2d objects

• A layer handles touch and accelerometer events

• A layer contains other layers and sprites

Monday, November 8, 2010

Page 16: Introduction to Cocos2d

Sprites

• A sprite (CCSprite) is an active object in your game

• 2 dimensional object

• Has an image (not generated)

• Can be animated, touched, moved, etc.

Monday, November 8, 2010

Page 17: Introduction to Cocos2d

Animated Sprites

• Use of Batch Sprites/Sprite Sheets/Atlas Sprites (versus individual image files)

• Depending on your version/history with Cocos2d

• Uses OpenGL Texture Atlas to provide higher animation performance

Monday, November 8, 2010

Page 18: Introduction to Cocos2d

Actions and Animation• You animate a sprite using the CCAnimation

class

• You make a sprite move using derivatives of the CCAction class

• You can specify sequences of actions (CCSequence) and actions done in parallel (CCSpawn)

Monday, November 8, 2010

Page 19: Introduction to Cocos2d

Block Game - a simple game to introduce you

to Cocos2d

Monday, November 8, 2010

Page 20: Introduction to Cocos2d

Commentary• It is important to know what you are building

before you build it...

• Design, look, interaction

• Cheaper and faster to design than to code

• ...but don’t go overboard

• Avoid “analysis paralysis”

• Iterate - design, code, test, rinse, repeat

Monday, November 8, 2010

Page 21: Introduction to Cocos2d

In short...

• Make it work

• Next, make it good

• Next, make it GREAT!

Monday, November 8, 2010

Page 22: Introduction to Cocos2d

Block Game

• Teach you about:

• Sprites

• Actions

• Collision detection

Monday, November 8, 2010

Page 23: Introduction to Cocos2d

Game Elements

Piece user drags to remove...

...one of the boxes moving

across the bottom of

the screen...

Monday, November 8, 2010

Page 24: Introduction to Cocos2d

Game Elements

...while avoiding the

blockers who are

protecting the boxes

Monday, November 8, 2010

Page 25: Introduction to Cocos2d

Additional Notes

• Single-color background

• I chose yellow, but you could pick another color

• You can use either color squares or dice images for UserSprite/boxes

• Both are included in basic sample project

• Using Cocos2d version 0.99.5 beta 3

Monday, November 8, 2010

Page 26: Introduction to Cocos2d

Game Items

• The piece the user moves around is a sprite (UserSprite)

• The boxes are sprites (BoxSprite)

• Blockers that move back and forth above and below the conveyor belt

Monday, November 8, 2010

Page 27: Introduction to Cocos2d

Game Actions• Touch and move UserSprite

• Boxes just move right-to-left

• New boxes are periodically added on right

• Blockers move back and forth

• UserSprite can collide with boxes and blockers

Monday, November 8, 2010

Page 28: Introduction to Cocos2d

Miscellanous

• Using XCode 3.2.5

• Using cocos2d-iphone-0.99.5-beta3

• Requires iOS 4.0+ (4.2 for iPad versions)

Monday, November 8, 2010

Page 29: Introduction to Cocos2d

Building the project

Monday, November 8, 2010

Page 30: Introduction to Cocos2d

Setting the scene

• Set orientation to portrait (Example 01)

• Set background color yellow (Example 02)

Monday, November 8, 2010

Page 31: Introduction to Cocos2d

Add the UserSprite

• Example 03

Monday, November 8, 2010

Page 32: Introduction to Cocos2d

Make UserSprite move

• Example 04

Monday, November 8, 2010

Page 33: Introduction to Cocos2d

Add the Boxes

• Example 05

Monday, November 8, 2010

Page 34: Introduction to Cocos2d

Collisions between UserSprite and Boxes

• Example 06

Monday, November 8, 2010

Page 35: Introduction to Cocos2d

Add the Blockers

• Example 07

Monday, November 8, 2010

Page 36: Introduction to Cocos2d

Miscellaneous changes

• Random start values - Example 08

• CocosDension (Sound) - Example 09

• iPad support - Example 10

Monday, November 8, 2010

Page 37: Introduction to Cocos2d

Miscellaneous

Monday, November 8, 2010

Page 38: Introduction to Cocos2d

For more information• http://cocos2d-iphone.org - Cocos2d for iOS information

• New book: Learn iPhone and iPad Cocos2D Game Development (Apress)

• Previous 360iDev Presentations, including:

• “Introduction to 2d Game Programming Using cocos2d” (360iDev Denver 2009)

• “Cocos2D + Box2D” (360iDev San Jose)

• Blogs (Ray Wenderlich’s is especially good)

Monday, November 8, 2010

Page 39: Introduction to Cocos2d

From this presentation

• Slides:

• http://dl.dropbox.com/u/5375467/Introduction%20to%20Cocos2d.key

• http://dl.dropbox.com/u/5375467/Introduction%20to%20Cocos2d.pdf

• Sample code:

• http://dl.dropbox.com/u/5375467/IntroToCocos2DSamples.zip

Monday, November 8, 2010

Page 40: Introduction to Cocos2d

My Contact Information

• Email: [email protected]

• Our company: http://beret.com

• Web: http://tomfrauenhofer.com

• Twitter: @tvf

Monday, November 8, 2010

Page 41: Introduction to Cocos2d

Thank you!

(and don’t forget the Game Jam!)

http://gamejam.360idev.com

Monday, November 8, 2010