SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
COCOS2D + BOX2D
  Creating physics game in 1 hour
WHO IS ROD?

• Founder
        of Prop Group
 www.prop.gr

• Background in enterprise
 software, now iPhone+iPad
 games!

• 2D physics game, Payload in
 the AppStore
In Progress...


www.cocos2dbook.com
SNOWBALL TOSS!
WHY COCOS2D

• Games       are fun! Making a game does not have to be hard.

• Write less infrastructure code, spend more time on design
  and core gameplay

• OpenGL  ES rendering and performance without having to
  learn OpenGL ES to get started

• It   is free!
GETTING COCOS2D




http://github.com/cocos2d/cocos2d-iphone
GETTING COCOS2D - PART 2
     1. Clone the Git Repository
     2. Install the Templates
XCODE TEMPLATES

• New   Project -> Cocos2D Templates
COCOS2D

• Objective-C   framework for games

• Scene   Management, Textures, Audio

• Everything   but the kitchen sink (3D stuff)*

• OpenGL   ES rendering and optimizations,
 Actions, Tile Maps, Parallax Scrolling, Scheduler,
 High Score service, ...
COCOS2D
                      ESSENTIALS

• Your game is divided into scenes, scenes into
 layers

• Layers   have what you care about, the Sprites

• Director   is used to switch between scenes

• Everything
          uses the CC namespace, so layers
 are CCLayers, CCScenes, CCSprites ...
LAYERS AND SCENES
              CCLayer
                           CCScene
              Gameplay

                           Gameplay
CCSprite(s)                 Scene
              CCLayer
              Background
LAYERS AND TOUCH
Accelerometer

                   CCLayer   CCScene

                   CCLayer   Gameplay
                              Scene
                   CCLayer


   Touch
MULTIPLE SCENES
CCLayer     CCScene

CCLayer      Gameplay       CCScene
              Scene
CCLayer
                            Director
CCLayer     CCScene
              Level
CCLayer     Completed
              Scene
CCLayer
COCOS2D
                           ACTIONS
• Actionsare an easy way to apply transitions, effects, and
 animations to your sprites

• MoveTo, MoveBy, ScaleBy, ScaleTo, FadeIn, FadeOut         ...
 CCAction *moveAction = [CCMoveBy actionWithDuration:2.0f
                    position:CGPointMake(50.0f,0.0f)];
 [playerSprite runAction:moveAction];




                                       2 seconds
COCOS2D+BOX2D
               ESSENTIALS 2
• Box2D    is C++

• 2Drigid physics simulation engine with
 continuous collision detection

• All
   your files that touch C++ or include it
 must be Objective-C++ (.mm)

• Tuned   for 1 meter sized objects!

• Use   a fixed time step!
A GAME IN 7 STEPS


• Let’s   begin
STEP 1

• Attach    the director to the AppDelegate
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];!// cocos2d will inherit these values
[window setMultipleTouchEnabled:YES];   // cocos2d will inherit these values
// create an openGL view inside a window
[[CCDirector sharedDirector] attachInView:window];!
[window makeKeyAndVisible];! !




• Director    Options
STEP 2


• init()

• createPhysicsWorld

• debug    draw

• ground   body
INIT()
    -(id)init {


!   if ((self=[super init])) {
!   !   CGSize screenSize = [CCDirector sharedDirector].winSize;
!   !
!   !   // enable touches
!   !   self.isTouchEnabled = YES;
!   !   // enable accelerometer
!   !   self.isAccelerometerEnabled = YES;
!   !
!   !   [self createPhysicsWorld];
!   !
!   !   [self addNewBodyWithCoords:ccp((screenSize.width/2)+80.0f, screenSize.height/2)
!   !   ! ! ! withDimensions:ccp(1.0f,0.5f)
!   !   ! ! ! ! andDensity:3.0f
!   !   ! ! !      andWithSprite:ICE_BLOCK_FILENAME_1];
!   !
!   !   // Start the scheduler to call the tick function
!   !   [self schedule: @selector(tick:)];
!   }
!   return self;
}
CREATEPHYSICSWORLD()
   // Define the gravity vector.
! b2Vec2 gravity;
! gravity.Set(0.0f, -10.0f);
!
! // Do we want to let bodies sleep?
! // This will speed up the physics simulation
! bool doSleep = true;
!
! // Construct a world object, which will hold and simulate the rigid bodies.
! world = new b2World(gravity, doSleep);
!
! world->SetContinuousPhysics(true);
  b2BodyDef groundBodyDef;
! groundBodyDef.position.Set(0, 0); // bottom-left corner
!
! // Call the body factory which allocates memory for the ground body
! // from a pool and creates the ground box shape (also from a pool).
! // The body is also added to the world.
! b2Body* groundBody = world->CreateBody(&groundBodyDef);
  b2PolygonShape groundBox;! !
!
! // bottom
! groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
! groundBody->CreateFixture(&groundBox);
STEP 3

• Create   the dynamic blocks
CODE+DEMO
Physics World + Dynamic Blocks
STEP 4

• Touch   Events
CODE+DEMO
 Touch Events Demo
STEP 5

• Time   to add graphics!
GRAPHICS DETAILS

• Background     Layer

• Sprites   for the static shapes

• Sprites   for the snowballs

• Penguin   animation

• Instructions   Layer
STEP 6


Let it snow! - Fun with
Particle Systems
STEP 7


Pump up the volume!
CLOSING THOUGHTS

• Sample   Code != Production Code

• Cocos2d Website:

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

 Look at the sample tests included with Cocos2D!
THANK YOU


• rod@prop.gr

• twitter.com/rodstrougo

• www.prop.gr

• www.cocos2dbook.com
EXTRAS

• SpriteSheets

• Creating    Physics Models

• Collision   detection

• Accelerometer     Filters

• Multi-touch   handling
HOW-TO PHYSICS MODELS


• VertexHelper    & Mekanimo

 http://github.com/jfahrenkrug/VertexHelper

 http://www.mekanimo.net/

• Ricardo’s   LevelSVG

 http://www.sapusmedia.com/levelsvg/

Mais conteúdo relacionado

Semelhante a Creating physics game in 1 hour

Demo creating-physics-game.
Demo creating-physics-game.Demo creating-physics-game.
Demo creating-physics-game.sagaroceanic11
 
Stefan stolniceanu spritekit, 2 d or not 2d
Stefan stolniceanu   spritekit, 2 d or not 2dStefan stolniceanu   spritekit, 2 d or not 2d
Stefan stolniceanu spritekit, 2 d or not 2dCodecamp Romania
 
Stefan stolniceanu spritekit, 2 d or not 2d
Stefan stolniceanu   spritekit, 2 d or not 2dStefan stolniceanu   spritekit, 2 d or not 2d
Stefan stolniceanu spritekit, 2 d or not 2dCodecamp Romania
 
Under Cocos 2 D Tree_mdevcon 2013
Under Cocos 2 D Tree_mdevcon 2013Under Cocos 2 D Tree_mdevcon 2013
Under Cocos 2 D Tree_mdevcon 2013Wooga
 
Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Maxim Zaks
 
cocos2d for i Phoneの紹介
cocos2d for i Phoneの紹介cocos2d for i Phoneの紹介
cocos2d for i Phoneの紹介Jun-ichi Shinde
 
Introduction to CocosSharp
Introduction to CocosSharpIntroduction to CocosSharp
Introduction to CocosSharpJames Montemagno
 
2 d gameplaytutorial
2 d gameplaytutorial2 d gameplaytutorial
2 d gameplaytutorialunityshare
 
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Joseph Labrecque
 
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo SwitchBringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo SwitchUnity Technologies
 
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobbmochimedia
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android GamesPlatty Soft
 
The not so short
The not so shortThe not so short
The not so shortAXM
 
CoreOS at Carnival
CoreOS at CarnivalCoreOS at Carnival
CoreOS at CarnivalToby Cox
 
CoreOS at Carnival
CoreOS at CarnivalCoreOS at Carnival
CoreOS at CarnivalToby Cox
 
Implementing CATiledLayer
Implementing CATiledLayerImplementing CATiledLayer
Implementing CATiledLayerJesse Collis
 

Semelhante a Creating physics game in 1 hour (20)

Demo creating-physics-game.
Demo creating-physics-game.Demo creating-physics-game.
Demo creating-physics-game.
 
Cocos2d 소개 - Korea Linux Forum 2014
Cocos2d 소개 - Korea Linux Forum 2014Cocos2d 소개 - Korea Linux Forum 2014
Cocos2d 소개 - Korea Linux Forum 2014
 
Stefan stolniceanu spritekit, 2 d or not 2d
Stefan stolniceanu   spritekit, 2 d or not 2dStefan stolniceanu   spritekit, 2 d or not 2d
Stefan stolniceanu spritekit, 2 d or not 2d
 
Stefan stolniceanu spritekit, 2 d or not 2d
Stefan stolniceanu   spritekit, 2 d or not 2dStefan stolniceanu   spritekit, 2 d or not 2d
Stefan stolniceanu spritekit, 2 d or not 2d
 
Under Cocos 2 D Tree_mdevcon 2013
Under Cocos 2 D Tree_mdevcon 2013Under Cocos 2 D Tree_mdevcon 2013
Under Cocos 2 D Tree_mdevcon 2013
 
Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013
 
cocos2d for i Phoneの紹介
cocos2d for i Phoneの紹介cocos2d for i Phoneの紹介
cocos2d for i Phoneの紹介
 
Introduction to CocosSharp
Introduction to CocosSharpIntroduction to CocosSharp
Introduction to CocosSharp
 
2 d gameplaytutorial
2 d gameplaytutorial2 d gameplaytutorial
2 d gameplaytutorial
 
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
 
Unity workshop
Unity workshopUnity workshop
Unity workshop
 
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo SwitchBringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
 
Basics cocos2d
Basics cocos2dBasics cocos2d
Basics cocos2d
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android Games
 
The not so short
The not so shortThe not so short
The not so short
 
CoreOS at Carnival
CoreOS at CarnivalCoreOS at Carnival
CoreOS at Carnival
 
CoreOS at Carnival
CoreOS at CarnivalCoreOS at Carnival
CoreOS at Carnival
 
Implementing CATiledLayer
Implementing CATiledLayerImplementing CATiledLayer
Implementing CATiledLayer
 

Último

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Creating physics game in 1 hour

  • 1. COCOS2D + BOX2D Creating physics game in 1 hour
  • 2. WHO IS ROD? • Founder of Prop Group www.prop.gr • Background in enterprise software, now iPhone+iPad games! • 2D physics game, Payload in the AppStore
  • 5. WHY COCOS2D • Games are fun! Making a game does not have to be hard. • Write less infrastructure code, spend more time on design and core gameplay • OpenGL ES rendering and performance without having to learn OpenGL ES to get started • It is free!
  • 7. GETTING COCOS2D - PART 2 1. Clone the Git Repository 2. Install the Templates
  • 8. XCODE TEMPLATES • New Project -> Cocos2D Templates
  • 9. COCOS2D • Objective-C framework for games • Scene Management, Textures, Audio • Everything but the kitchen sink (3D stuff)* • OpenGL ES rendering and optimizations, Actions, Tile Maps, Parallax Scrolling, Scheduler, High Score service, ...
  • 10. COCOS2D ESSENTIALS • Your game is divided into scenes, scenes into layers • Layers have what you care about, the Sprites • Director is used to switch between scenes • Everything uses the CC namespace, so layers are CCLayers, CCScenes, CCSprites ...
  • 11. LAYERS AND SCENES CCLayer CCScene Gameplay Gameplay CCSprite(s) Scene CCLayer Background
  • 12. LAYERS AND TOUCH Accelerometer CCLayer CCScene CCLayer Gameplay Scene CCLayer Touch
  • 13. MULTIPLE SCENES CCLayer CCScene CCLayer Gameplay CCScene Scene CCLayer Director CCLayer CCScene Level CCLayer Completed Scene CCLayer
  • 14. COCOS2D ACTIONS • Actionsare an easy way to apply transitions, effects, and animations to your sprites • MoveTo, MoveBy, ScaleBy, ScaleTo, FadeIn, FadeOut ... CCAction *moveAction = [CCMoveBy actionWithDuration:2.0f position:CGPointMake(50.0f,0.0f)]; [playerSprite runAction:moveAction]; 2 seconds
  • 15. COCOS2D+BOX2D ESSENTIALS 2 • Box2D is C++ • 2Drigid physics simulation engine with continuous collision detection • All your files that touch C++ or include it must be Objective-C++ (.mm) • Tuned for 1 meter sized objects! • Use a fixed time step!
  • 16. A GAME IN 7 STEPS • Let’s begin
  • 17. STEP 1 • Attach the director to the AppDelegate window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setUserInteractionEnabled:YES];!// cocos2d will inherit these values [window setMultipleTouchEnabled:YES]; // cocos2d will inherit these values // create an openGL view inside a window [[CCDirector sharedDirector] attachInView:window];! [window makeKeyAndVisible];! ! • Director Options
  • 18. STEP 2 • init() • createPhysicsWorld • debug draw • ground body
  • 19. INIT() -(id)init { ! if ((self=[super init])) { ! ! CGSize screenSize = [CCDirector sharedDirector].winSize; ! ! ! ! // enable touches ! ! self.isTouchEnabled = YES; ! ! // enable accelerometer ! ! self.isAccelerometerEnabled = YES; ! ! ! ! [self createPhysicsWorld]; ! ! ! ! [self addNewBodyWithCoords:ccp((screenSize.width/2)+80.0f, screenSize.height/2) ! ! ! ! ! withDimensions:ccp(1.0f,0.5f) ! ! ! ! ! ! andDensity:3.0f ! ! ! ! ! andWithSprite:ICE_BLOCK_FILENAME_1]; ! ! ! ! // Start the scheduler to call the tick function ! ! [self schedule: @selector(tick:)]; ! } ! return self; }
  • 20. CREATEPHYSICSWORLD() // Define the gravity vector. ! b2Vec2 gravity; ! gravity.Set(0.0f, -10.0f); ! ! // Do we want to let bodies sleep? ! // This will speed up the physics simulation ! bool doSleep = true; ! ! // Construct a world object, which will hold and simulate the rigid bodies. ! world = new b2World(gravity, doSleep); ! ! world->SetContinuousPhysics(true); b2BodyDef groundBodyDef; ! groundBodyDef.position.Set(0, 0); // bottom-left corner ! ! // Call the body factory which allocates memory for the ground body ! // from a pool and creates the ground box shape (also from a pool). ! // The body is also added to the world. ! b2Body* groundBody = world->CreateBody(&groundBodyDef); b2PolygonShape groundBox;! ! ! ! // bottom ! groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0)); ! groundBody->CreateFixture(&groundBox);
  • 21. STEP 3 • Create the dynamic blocks
  • 22. CODE+DEMO Physics World + Dynamic Blocks
  • 25. STEP 5 • Time to add graphics!
  • 26. GRAPHICS DETAILS • Background Layer • Sprites for the static shapes • Sprites for the snowballs • Penguin animation • Instructions Layer
  • 27. STEP 6 Let it snow! - Fun with Particle Systems
  • 28. STEP 7 Pump up the volume!
  • 29. CLOSING THOUGHTS • Sample Code != Production Code • Cocos2d Website: http://www.cocos2d-iphone.org/ Look at the sample tests included with Cocos2D!
  • 30. THANK YOU • rod@prop.gr • twitter.com/rodstrougo • www.prop.gr • www.cocos2dbook.com
  • 31. EXTRAS • SpriteSheets • Creating Physics Models • Collision detection • Accelerometer Filters • Multi-touch handling
  • 32. HOW-TO PHYSICS MODELS • VertexHelper & Mekanimo http://github.com/jfahrenkrug/VertexHelper http://www.mekanimo.net/ • Ricardo’s LevelSVG http://www.sapusmedia.com/levelsvg/