27
Cocos2D for iOS

Presentation 121223112735-phpapp01

Embed Size (px)

Citation preview

Page 1: Presentation 121223112735-phpapp01

Cocos2Dfor iOS

Page 2: Presentation 121223112735-phpapp01

What is Cocos2DPopular Objective-C game engine

Easy to use

Tons of support

Open source and free (Very important)

Page 3: Presentation 121223112735-phpapp01

Cocos2DUsed in thousands of games

Creator of Cocos2D was acqui-hired by Zynga

Yes, the Zynga whose stock is tanking now

Page 4: Presentation 121223112735-phpapp01
Page 5: Presentation 121223112735-phpapp01

What is Cocos2DHas a lot of ports

Cocos2d-x for Android and cross platform

Cocos2d html5

Cocos2d for Blackberry etc

Page 6: Presentation 121223112735-phpapp01

What is Cocos2Dhttp://www.cocos2d-iphone.org/download

Go download it now

Page 7: Presentation 121223112735-phpapp01

Cocos2DDirector – The guy that calls the shots

Scenes – Director manages scenes

Layers – Scenes can comprise of 1 or more layers

It is a game engine based on concept of nodes

Page 8: Presentation 121223112735-phpapp01

Cocos2D

Page 9: Presentation 121223112735-phpapp01

ScenesThink of a scene as a screen

So in a game, you may have a• Starting scene• Gameplay scene• Results sceneetc

Page 10: Presentation 121223112735-phpapp01

LayersA scene can have > 1 layers

Think of a layer in the same vein as a layer in a Photoshop PSD file

Same concept

Page 11: Presentation 121223112735-phpapp01

CCNodeCCNode is the base class that almost all Cocos2D classes inherit from

This has common properties such as - Position- Z order- Tag- etc

Page 12: Presentation 121223112735-phpapp01

OriginThe coordinate system starts from bottom left of screen

0,0 X

Y

Hope you still remember your Cartesian Math

Page 13: Presentation 121223112735-phpapp01

Cocos2DAll classes and objects have the prefix ‘CC’

• CCSprite

• CCAction

• CCMoveBy

etc

Page 14: Presentation 121223112735-phpapp01

CCSpriteAn image object that loads in a png file

CCSprite *sprite = [CCSprite spriteWithFile:@”man.png”];

Page 15: Presentation 121223112735-phpapp01

Sprite MovementCocos2d has a vast array of actions

• Movement• Scaling• Fading• Rotation• etc• http://www.cocos2d-iphone.org/api-

ref/2.1.0/interface_c_c_action.html

Page 16: Presentation 121223112735-phpapp01

Sprite• After loading a sprite• Need call the ‘add’ method• To add the sprite to another sprite• Or add to a scene• Then the game engine handles

the rendering for you• Easy as Pie

Page 17: Presentation 121223112735-phpapp01

Sprite Basics

CCSprite *sprite = [CCSprite spriteWithFile:@”man.png”];• [self addChild:sprite];• [sprite runAction:[CCMoveTo

actionWithDuration: 2.0f position:ccp(240, 320)]];

This moves man.png to position 240, 320 using 2 seconds

Page 18: Presentation 121223112735-phpapp01

UIKitSo what happens if you want to integrate UIKit element?

• Textfield

• Alertview

• Webview

Can you do that with Cocos2D?

Page 19: Presentation 121223112735-phpapp01

UIKitYes, you can. Most common way is to add them in using code

UIAlertView* alert_view = [[UIAlertView alloc] initWithTitle:@”Title" message:@"Your Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];[alert_view setDelegate:self];[alert_view show];[alert_view autorelease];

Page 20: Presentation 121223112735-phpapp01

UIKit

Page 21: Presentation 121223112735-phpapp01

UIKitYes, you can. Most common way is to add them in using code

UIView *myView = (UIView*) [[CCDirector sharedDirector] openGLView];//get the view textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 168, 250, 40)]; [myView addSubview:textField]; [[[[CCDirector sharedDirector]openGLView] window]addSubview:myView];[myView setDelegate:self];

Page 22: Presentation 121223112735-phpapp01

UIKit

Page 23: Presentation 121223112735-phpapp01

UIKitYes, you can also add your xibs into Cocos2D

http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

Page 24: Presentation 121223112735-phpapp01

Audio

• To play audio is very simple• Just #import “SimpleAudioEngine.h”• Then call the methods to play the audio• Background music• Effect

• mp3, wav, caf (Core Audio Format) files can be used

Page 25: Presentation 121223112735-phpapp01

Audio

• [[SimpleAudioEngine sharedEngine] preloadEffect:@"sfx.wav"];

//sfx.wav is the name of the sound file

• [[SimpleAudioEngine sharedEngine] playEffect:@"sfx.wav"];//play the audio file sfx.wav

• [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bgm.mp3"];//play the bgm.mp3 background music

Page 26: Presentation 121223112735-phpapp01

It’s Demo TimeI am going to show you how to create a simple game

- Game mechanics is simple- Tap the snails to squash them- Before they reach the end of the screen- It won’t be the next Angry Birds- But it’s a start

Page 27: Presentation 121223112735-phpapp01

Download Code

•http://www.azukisoft.com/HelloiOSDevs.zip