iOS Gaming with Cocos2d

49
iOS gaming with cocos2d Prepared by Ngo Duc Hiep Copyright 2011 PTT Solution ., JSC. All rights reserved. Hanoi, May 2011

Transcript of iOS Gaming with Cocos2d

Page 1: iOS Gaming with Cocos2d

iOS gaming with cocos2d

Prepared by Ngo Duc Hiep

Copyright 2011 PTT Solution ., JSC. All rights reserved.

Hanoi, May 2011

Page 2: iOS Gaming with Cocos2d

iOS game engines

Sparrow

Galaxyoolong

cocos2d

Page 3: iOS Gaming with Cocos2d

iOS game engines

Corona

iTorque

Game salad

Unity

Page 4: iOS Gaming with Cocos2d

iOS game engines

http://maniacdev.com/2011/01/open-source-and-commercial-ios-game-engine-listings-updated/

http://maniacdev.com/2009/09/the-commercial-iphone-game-engine-comparison-3d-and-2d/

Page 5: iOS Gaming with Cocos2d

cocos2d games

Tap Pet Hotel: #1 Free

Tap Zoo: #2 Free

Zombie Farm: #6 Free

iStunt 2: #6 Free

Page 6: iOS Gaming with Cocos2d

cocos2d games

Trainyard: #2 Paid

ZombieSmash: #1 Paid

Air Penguin: #1 Paid

Fishing Frenzy: #7 Paid

Page 7: iOS Gaming with Cocos2d

cocos2d

Page 8: iOS Gaming with Cocos2d

App workflow

Loading Home

Leaderboard

Game Play

Settings

Page 9: iOS Gaming with Cocos2d

App workflow

CCScene

CCDirector: run, push, pop replace scene

[[CCDirector sharedDirector] runWithScene:loading];

[[CCDirector sharedDirector] replaceScene:home];

CCTransition: transition between scenes

Page 10: iOS Gaming with Cocos2d

CCLayer

Page 11: iOS Gaming with Cocos2d

CCLayer

Handle event

isTouchEnabled_

isAccelerometerEnabled_

Game controller

Mange game entities - CCNode

Page 12: iOS Gaming with Cocos2d

CCNode

Renderable item

CCLayer, CCScene are all nodes

Features

Contains another nodes

Schedule timer

Play actions

Position/rotation/scale/contentSize

Page 13: iOS Gaming with Cocos2d

CCSprite

Sprites

Texture

Page 14: iOS Gaming with Cocos2d

CCNode: CoordinatecontentSize.width

conte

ntS

ize.h

eig

ht

Page 15: iOS Gaming with Cocos2d

CCNode: Coordinate

rotation

positio

nanchorPoint

Page 16: iOS Gaming with Cocos2d

CCNode: Coordinate

Page 17: iOS Gaming with Cocos2d

CCNode: Transformation

- (CGAffineTransform)nodeToParentTransform;

- (CGAffineTransform)parentToNodeTransform;

- (CGAffineTransform)nodeToWorldTransform;

- (CGAffineTransform)worldToNodeTransform;

- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;

- (CGPoint)convertToWorldSpace:(CGPoint)nodePoint;

Page 18: iOS Gaming with Cocos2d

CCNode: Actions

Modify node’s attributes by time

position/rotation/scale/opacity/grid

Interval actions and Instant actions

Page 19: iOS Gaming with Cocos2d

CCNode: Actions

CCMove_

CCJump_

CCBezier_

CCScale_

CCRotate_

CCFade_

Page 20: iOS Gaming with Cocos2d

CCNode: Actions

Time alter actions:

• CCEase_

• CCEaseExponential_

• CCEaseSine_

• CCEaseElastic_

• CCEaseBounce_

Page 21: iOS Gaming with Cocos2d

CCNode: Special actions

CCCallFunc

CCCallBlock

CCPropertyAction

CCFollow

CCSequence

CCSpawn

CCRepeat

Page 22: iOS Gaming with Cocos2d

CCNode: 3D Effects

CCFlipX3D/CCFlipY3D

CCWaves3D

CCRipple3D

CCSplitRows

Page 23: iOS Gaming with Cocos2d

CCSprite: Animation

Page 24: iOS Gaming with Cocos2d

CCSprite: Animation

Page 25: iOS Gaming with Cocos2d

CCNode: Scheduler

-(void) schedule: (SEL) s interval:(ccTime)seconds;

-(void) unschedule: (SEL) s;

-(void) pauseSchedulerAndActions;

-(void) resumeSchedulerAndActions;

Page 26: iOS Gaming with Cocos2d

Touch Dispathcher

CCTouchDispathcher

-(void) addStandardDelegate:(id) delegate priority:(int)priority;

-(void) addTargetedDelegate:(id) delegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches;

-(void) removeDelegate:(id) delegate;

Page 27: iOS Gaming with Cocos2d

Box2D

Page 28: iOS Gaming with Cocos2d

Features

Rigid body Fixture: rectangle, circle, poligon

Joint

Contact filter

Contact listener

Simulate

world->Step(dt);

Page 29: iOS Gaming with Cocos2d

cocos2d Box2D

Body, joint, fixture user data

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())

{

if (b->GetUserData() != NULL) {

CCSprite *actor = (CCSprite*)b->GetUserData();

actor.position = CGPointMake( b->GetPosition().x , b->GetPosition().y);

actor.rotation = -b->GetAngle() * 180 / M_PI;

}

}

Page 30: iOS Gaming with Cocos2d

Car

Cocos2d

ccCarBody

ccCarWheel1

ccCarWhell2

Box2D

b2CarBody

b2CarWheel1

b2CarWhell2

b2CarJoint1

b2CarJoint2

Page 31: iOS Gaming with Cocos2d

cocos2d+ext

Page 32: iOS Gaming with Cocos2d

CCNode+ext

Transform

- (CGAffineTransform) nodeToNodeTransform:

(CCNode *) node;

- (CGAffineTransform) nodeFromNodeTransform:

(CCNode *) node;

Page 33: iOS Gaming with Cocos2d

CCNode+ext

Transform

Collision

- (CGRect) rect;

- (BOOL) isCollideWithNode:(CCNode *) node;

Page 34: iOS Gaming with Cocos2d

CCNode+ext

Transform

Collision

Containing

- (BOOL) containsPoint:(CGPoint) loc;

- (BOOL) containsTouch:(UITouch *) touch;

Page 35: iOS Gaming with Cocos2d

CCSprite+ext - (CCAction *) animateFramesWithKey:(NSString *) key

startIndex:(int) start

endIndex:(int) end

duration:(float) duration

reverse:(BOOL) reverse

forever:(BOOL) forever;

Page 36: iOS Gaming with Cocos2d

CCComponent

Independent, portable

States control

Data driven

Callbacks

Physics support

Composition

Page 37: iOS Gaming with Cocos2d

CCComponent

Data

Texture file

Texture def file

Component def fileStatesChilds

Page 38: iOS Gaming with Cocos2d

CCComponent

States

Animate frames

Rotate

Scale

Fade

Page 39: iOS Gaming with Cocos2d

CCComponent

Lion_jump_1.png

Lion_jump_4.pngLion_jump_3.png

Lion_jump_2.png

Page 40: iOS Gaming with Cocos2d

CCComponent

Lion “jump” state:

key=Lion_jump_

type=frame

startframe=1

endframe=4

duration=0.1

forever=NO

lion.state = @”jump”;

Page 41: iOS Gaming with Cocos2d

CCComponent

- (void) setState:(NSString *) state;

- (void) setState:(NSString *) state target:(NSString *) target selector:(SEL) selector;

- (void) spawnStates:(NSString *) state1,…;

- (void) setStateSequence:(NSString *) state1, …;

Page 42: iOS Gaming with Cocos2d

CCLayer+ext

Actors group addChild:(CCNode *) child group:(int) gid; removeChild:(CCNode *) child group:(int) gid;

Page 43: iOS Gaming with Cocos2d

CCLayer+ext

Contacts listener addContactListenerBetweenGroup:(int) g1 andGroup:(int)

g2 selector:(SEL) listener;

- (void) hero:(Hero *) hero collideWith:(Bullet *) bullet{

hero.blood -= bullet.effect;

[bullet explode];

}

Page 44: iOS Gaming with Cocos2d

CCLayer+ext

Touch helper - (CGPoint) pointForTouches:(NSSet *) touches; - (CGPoint) pointForTouch:(UITouch *) touch; - (CCNode *) getActorAtPoint:(CGPoint) pos; - (CCNode *) getActorAtTouch:(UITouch *) touch;

Page 45: iOS Gaming with Cocos2d

Tools

Page 46: iOS Gaming with Cocos2d

Texture: texturetool, TexturePacker, Zwoptex

Tile map editor: Tiled

SVG editor: Inkscape, svg-edit Level design

Particle: Particle Designer

Font editor: Hiero

Audio: Audacity

3D: Blender

Page 47: iOS Gaming with Cocos2d

Learning resources

Page 49: iOS Gaming with Cocos2d

Demo & QA