2013-04-16 cocos2d & chipmunk

Click here to load reader

  • date post

    30-Nov-2014
  • Category

    Technology

  • view

    904
  • download

    6

Embed Size (px)

description

 

Transcript of 2013-04-16 cocos2d & chipmunk

  • 1. cocos2d & Chipmunk2D games development for iOSKonrad Koakowski, Playsoft
  • 2. cocos2dWhat is it?
  • 3. cocos2d - what was made with it?Kingdom Rush
  • 4. cocos2d - what was made with it?Robot Unicorn Attack
  • 5. cocos2d - what was made with it?League of Evil
  • 6. ChipmunkWhat is it?
  • 7. Chipmunk - what was made with it?Waking Mars
  • 8. Chipmunk - what was made with it?Feed me Oil
  • 9. Chipmunk - what was made with it?I Dig It
  • 10. Creating project in Xcode1. Download cocos2d-iphone: www.cocos2d-iphone.org/download2. If you want to comfortably creating newgames, install templates for Xcode ( run thiscommand in Terminal ):./install-templates.sh -f -u
  • 11. Creating project in XcodeThats all, in this moment, after creating aproject you should see following options:
  • 12. Creating project in XcodeAs you can see cocos2d delivers Chipmunkcode in theirs templates.Beside Chipmunk there is also version withBox2D integrated, similar 2D physics.
  • 13. cocos2d basics - layers, nodescocos2d logic is divided to scenes. Scene is amain node to which we connect next ones (layers, sprites, particle effects etc. )Each element added to the game ( exceptscenes ) have to be added to some parentelement.
  • 14. Podstawy cocos2d - warstwy, wzy
  • 15. cocos2d basics - layers, nodes
  • 16. cocos2d basics - coordinatesCoordinate system, anchor points.
  • 17. cocos2d basics - adding elementsHow to draw such an alien?// GameScene.h#import "cocos2d.h"@interface GameScene : [email protected]
  • 18. // GameScene.m#import "GameScene.h"@implementation GameScene-(id) init {self = [super init];if(self) {// 1CGSize s = [CCDirector sharedDirector].winSize;// 2CCSprite* invader = [CCSprite spriteWithFile: @"invader.png" ];invader.position = ccp(s.width/ 2, s.height/2);// 3[self addChild: invader];}return self;}@endcocos2d basics - adding elements
  • 19. To enable gathering touch events on our layer:[self setIsTouchEnabled: YES];Next we need to implement CCStandardTouchDelegatemethods:- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;- (void)ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;- (void)ccTouchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;cocos2d basics - touch support
  • 20. cocos2d basics - touch support- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{for( UITouch *touch in touches ) {CGPoint pos = [touch locationInView: [touch view]];pos = [[CCDirector sharedDirector] convertToGL:location];// Do something with position// (...)}}
  • 21. cocos2d basics - actionsActions are a nice element of cocos2dframework. They allow to easily createuncomplicated animations or repeated logic.Especially with Obj-C blocks.We can join actions in sequences, addingeasing etc.
  • 22. cocos2d basics - actions
  • 23. cocos2d basics - actions
  • 24. Following code will cause that our invader willturn around and move by 50 points. Everythingin one second.-(void) roll {CCMoveBy* move = [CCMoveBy actionWithDuration: 1.0fposition: ccp( 50.0f, 0.0f)];CCRotateBy* rotate = [CCRotateBy actionWithDuration: 1.0fangle: 360.0f];[invader_ runAction: move];[invader_ runAction: rotate];}cocos2d basics - actions
  • 25. - creating/destroying all objects should beperformed through cpSpaceNew,cpSpaceFree, cpBodyNew, etc.- to configure and create a space:cpSpace* space = cpSpaceNew();cpSpaceSetGravity(space, ccp(0.0f, -500.0f));Chipmunk basics - creating a space
  • 26. Chipmunk basics - physical objectsConcept of shape and body:A body is a physical state ( mass, position,rotation... ) and shapes are used forrecognizing collisions.
  • 27. Creating a "floor"// 1CGSize s = [CCDirector sharedDirector].winSize;CGPoint lowerLeft = ccp(0, 0);CGPoint lowerRight = ccp(s.width, 0);float height = 20.0f;// 2cpBody* groundBody = cpBodyNewStatic();// 3cpShape* groundShape = cpSegmentShapeNew(groundBody, lowerLeft, lowerRight,height);cpShapeSetElasticity(groundShape, 0.2f);cpShapeSetFriction(groundShape, 1.0f);// 4cpSpaceAddShape(space, groundShape);Chipmunk basics - physical objects
  • 28. Creating and adding a rectangle object:static const float boxW = 30.0f;static const float boxH = 50.0f;static const float mass = 2.5f;cpBody* body = cpBodyNew(mass, cpMomentForBox(mass, boxW,boxH));cpBodySetPosition(body, ccp(s.width/2, s.height/2));cpSpaceAddBody(space, body);cpShape* shape = cpBoxShapeNew(body, boxW, boxH);cpShapeSetElasticity(shape, 0.8f);cpShapeSetFriction(shape, 1.0f);cpSpaceAddShape(space, shape);Chipmunk basics - physical objects
  • 29. Chipmunk basics - starting asimulation// Inside a layer init method[self scheduleUpdate];// update is called every frame-(void) update:(ccTime)dt {cpSpaceStep(space, dt);}
  • 30. SummaryThose are only the basics of those twoframeworks, more things are waiting todiscover!Official documentation:chipmunk-physics.net/release/ChipmunkLatest-Docswww.cocos2d-iphone.org/api-ref/latest-stable
  • 31. Questions?
  • 32. Konrad [email protected]