Cocos2d game programming 2

67
Cocos2D

description

Texas A&M University의 Android Programming Class의 Guest Talk 자료입니다.

Transcript of Cocos2d game programming 2

Page 1: Cocos2d game programming 2

Cocos2D

Page 2: Cocos2d game programming 2

Cocos2d

• Open Source 2D Game Framework

• Built upon the OpenGL API’s

• 2D Game, Graphic, Interactive Application Programs

• Sep. 2008 - 0.3.0 version released

• Used in thousands of apps

• Active developer community

Page 3: Cocos2d game programming 2

Cocos2d

• Multi-platform game engine

• iOS, Mac OS X, Android ported

• Cocos2d-x multi-platform port written in C++

Page 4: Cocos2d game programming 2

iOS game engines(non-commercial)

Sparrow(open source)

Galaxy(New BSDLicense)

oolong engine(MIT License)

Cocos2d(open source)

Page 5: Cocos2d game programming 2

iOS game engines(commercial)

Unreal Dev Kit

SIO2

Game salad

Unity

And much more...

Page 7: Cocos2d game programming 2

Cocos2d games

Tap Pet Hotel: #1 Free

Tap Zoo: #2 Free

Zombie Farm: #6 Free

iStunt 2: #6 Free

Page 8: Cocos2d game programming 2

Trainyard: #2 Paid

ZombieSmash: #1 PaidAir Penguin: #1 Paid

Fishing Frenzy: #7 Paid

Cocos2d games

Page 9: Cocos2d game programming 2

Cocos2d for Mac App

Page 10: Cocos2d game programming 2

Cocos2d vs OpenGL

• You can make a high level game using openGL

• You will need an animation of sprits, menus for the game, actions for game objects, sound effects, and much more...

• Cocos2d supports a wrapper class of OpenGL

• Also sound control class, menu class, sprites...

Page 11: Cocos2d game programming 2

Cocos2d

http://www.cocos2d-iphone.org/

Page 12: Cocos2d game programming 2

Installation for iOS

Page 13: Cocos2d game programming 2

Installation for iOS

Page 14: Cocos2d game programming 2

Installation(super easy)

Page 15: Cocos2d game programming 2

Cocos2d for Android

Page 16: Cocos2d game programming 2

Sample Program

Many featured sample codes are provided

Page 17: Cocos2d game programming 2

Cocos2D Package

Tutorial codes for beginners-easy to start

Page 18: Cocos2d game programming 2

Tutorial

Page 19: Cocos2d game programming 2

Demo

Cocos2D for iPhone

Page 20: Cocos2d game programming 2

and More

Page 21: Cocos2d game programming 2

Features• Scene Graph Structure

• Flow Control(Scene Management)

• Flow Control between Scenes

• Sprites and Sprite Sheets

• Easy and powerful way of Sprite Usage

• Action

• Supports many actions including move, rotate, scale, sequence, spawn,...

Page 22: Cocos2d game programming 2

Features

• Effect

• Various 2D effect including wave, twirl, lens, liquid, ripple

• Tiled Maps, Transition, Menus, Text Rendering

• Physics Engine : Chipmunk, Box2d, Particle System

Page 23: Cocos2d game programming 2

Features

• Basic Menu and Button

• Parallax scrolling

• Sound Controls, Particle System

• High Score server(Cocos live)

• Based on OpenGL ES 1.1 - for iOS

• Based on OpenGL 1.5 - for Mac

Page 24: Cocos2d game programming 2

Scene Graph

• Hierarchy of every Cocos2d NODE that's currently active.

• Every node has exactly one parent node, except the scene itself, and can have any number of child nodes.

• General technique on high level graphics system or game programming

Page 25: Cocos2d game programming 2

Scene GraphCCScene

CCLayer CCLayer CCLayer

CCSprite CCMenu CCSprite

Page 26: Cocos2d game programming 2

Scene Graph Structure

• General data structure used in Vector based graphics editing application or games.

• Acrobat 3D, Java 3D, AutoCAD, VRML, Open Inventor,..

• Collection of nodes in a graph or tree structure

Page 27: Cocos2d game programming 2

Object class in Java

• The Object class is the super class for all classes in Java

• Defines the basic states and behavior that all objects must have

• equals, getClass, toString, ...

Page 28: Cocos2d game programming 2

CCNode in Cocos2d

• The CCNode class is the super class of all Cocos2d class

• Defines common attributes and methods that all Cocos2d object must have

Page 29: Cocos2d game programming 2

CCNode Class

• Most important Cocos2D class, most object in Cocos2d inherits from CCNode

• CCScene, CCLayer, CCSprite, CCMenu classes are children of CCNode class

• Can include another CCNode object.

• Can schedule periodic callbacks

• Can run all actions

Page 30: Cocos2d game programming 2

CCNode Class

• Attributes

• rotation, scaleX_, scaleY_,position, visible_, anchorPointInPixels_, anchorPoint_, isRelativeAnchorPoint_, contentSize_, transform_, inverse_, vertexZ_, camera_, grid, zOrder_, children_

Page 31: Cocos2d game programming 2

CCNode: Attributes

rotation

posit

ion

anchorPoint

• If you want to draw a monster in your game scene• You will need an image( CCSprite )• And its position, size, rotation angle, anchor point, ...

Page 32: Cocos2d game programming 2

CCDirector Class

• Cocos2D Class has name space starting with CCxxx.

• Director(CCDirector)

• Management object for game control thru scene

• OpenGL ES environment setting

• Running the game loop

Page 33: Cocos2d game programming 2

CCScene Class

• Scene

• A game is composed of multiple screens

• Each scene compose each screen on game

• Scene is composed of multiple layers(hierarchical structure)

Page 34: Cocos2d game programming 2

Scene & Director

CCDirector

CCScene CCScene CCScene..

..

Page 35: Cocos2d game programming 2

CCScene ClassCCScene

CCLayer CCLayer

(game layer) (touch handling layer)

Page 36: Cocos2d game programming 2

CCLayer Class

• One Game scene can contain many layers

• and each layer contains more than one sprite or menu

• Capture user interaction or accelerometer

• Layer consists a scene

Page 37: Cocos2d game programming 2

CCLayerScore: 210 LIFE : 3

CCLayer

CCSprite CCLabel CCNode..

CCSprite CCSprite ..

..

Page 38: Cocos2d game programming 2

CCLayerScore: 210 LIFE : 3

CCLayer

CCSprite CCLabel CCNode..

CCSprite CCSprite ..

..

Page 39: Cocos2d game programming 2

• CCLayer can handle events

• isTouchEnabled_

• isAccelerometerEnabled_

• Game controller

• Manage game entities

CCLayer

Page 40: Cocos2d game programming 2

Sprite Object

• Image files are loaded into OpenGL ES Textures in a format that the GPU can understand

• Sprites can move within a scene or a layer

• Sprites may be animated, by cycling through a number of different images

Page 41: Cocos2d game programming 2

CCSprite

Texture-Sprite Sheet

Sprites

Page 42: Cocos2d game programming 2

Action

• Control the movement, transition, and effects of Cocos2d objects.

• All CCNode objects are able to run actions.

• CCAnimate, CCJumpBy, CCRepeatForever, CCSequence, CCSpawn, CCMoveTo, CCFadeTo,...

Page 43: Cocos2d game programming 2

CCNode: Actions• Modify node’s attributes by time

• position/rotation/scale/opacity/grid/...

• Interval actions and Instant actions

Page 44: Cocos2d game programming 2

Running an Action

high level action description

Page 45: Cocos2d game programming 2

Tools - TexturePacker

• Loading a sprite requires much system resources

• In a game scene many sprites are required

• each sprite contains many animation images

• packaging each image into a big sprite sheet

Page 46: Cocos2d game programming 2

Sprite Images

...

MB_01.png MB_02.png MB_03.png MB_03.png

Page 47: Cocos2d game programming 2

Making a big Sprite Sheet

Page 48: Cocos2d game programming 2

Meta data

Page 49: Cocos2d game programming 2

in your codeSprite Sheet file

Sprite file

Page 50: Cocos2d game programming 2

Tools-Font Generator

• Many game developers want in-house bitmap font

• Game scene needs an special font effect and customized fonts

Page 51: Cocos2d game programming 2

Font Generator

Page 52: Cocos2d game programming 2

in your code

font file(testFont.png)

import meta-data(testFont.fnt)

Page 53: Cocos2d game programming 2

Sound Editor• Sound Effect is very important in a game

Page 54: Cocos2d game programming 2

Map Editor

Page 55: Cocos2d game programming 2

Particle Editor

Page 56: Cocos2d game programming 2

More game tools

http://www.learn-cocos2d.com/2011/06/complete-list-cocos2d-tools/

Page 57: Cocos2d game programming 2

Game Tools

• Many 3rd party game tools for Cocos2d are available

• Enrich your game effect

• Minimize your programming efforts

• More efficient way of memory management

Page 58: Cocos2d game programming 2

Cocos2d for Android

Page 59: Cocos2d game programming 2

Download

• Unzip downloaded file

• “ZhouWeikuan-cocos2d-1d79d40” folder will be generated.

Page 62: Cocos2d game programming 2

Android

Page 63: Cocos2d game programming 2

Android code

Page 64: Cocos2d game programming 2

iOS

Page 65: Cocos2d game programming 2

iOS code

Page 66: Cocos2d game programming 2

Demo

Page 67: Cocos2d game programming 2

Q & A