iPhone game programming with Cocos2D

62ec120256167ee34435f007becc2c13?s=47 Christian Stigen Larsen
October 11, 2011
29

iPhone game programming with Cocos2D

Norwegian presentation at Bouvet on iPhone game programming using Cocos2D and physics engines.

62ec120256167ee34435f007becc2c13?s=128

Christian Stigen Larsen

October 11, 2011
Tweet

Transcript

  1. iPhone spillutvikling med Christian Stigen Larsen, CODE

  2. None
  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. • iOS

  14. • iOS • Cocos2D + fysikkmotorer

  15. • iOS • Cocos2D + fysikkmotorer • Spill i Objective-C

  16. iOS

  17. iOS Devices

  18. iOS Devices

  19. iOS Devices

  20. iOS Devices

  21. iOS SDK

  22. iOS SDK Core OS

  23. iOS SDK Core OS Core Services

  24. iOS SDK Core OS Core Services Media Layer

  25. iOS SDK Core OS Core Services Media Layer Cocoa Touch

  26. iOS SDK Cocos2D Core OS Core Services Media Layer Cocoa

    Touch
  27. Cocos2D konsepter

  28. Cocos2D konsepter • Sprites + actions

  29. Cocos2D konsepter • Sprites + actions • Layers

  30. Cocos2D konsepter • Sprites + actions • Layers • Scenes

  31. Cocos2D konsepter • Sprites + actions • Layers • Scenes

    • Director
  32. Cocos2D konsepter • Sprites + actions • Layers • Scenes

    • Director • Fysikkmotor
  33. Sprites

  34. Sprites

  35. Sprites

  36. Sprites

  37. Sprites

  38. Sprites

  39. Sprites

  40. Sprites

  41. Sprites

  42. Sprites

  43. Sprites

  44. Layers

  45. Layers

  46. Layers

  47. Layers

  48. Layers

  49. Layers

  50. Layers

  51. Layers

  52. Layers

  53. Layers

  54. Layers

  55. Layers

  56. Layers

  57. Layers

  58. Layers

  59. Scenes

  60. Scenes Hovedmeny

  61. Scenes Hovedmeny Level 1 ... n

  62. Scenes Hovedmeny Level 1 ... n Game Over

  63. Director Hovedmeny Level 1 ... n Game Over

  64. Director Hovedmeny Level 1 ... n Game Over

  65. Director Hovedmeny Level 1 ... n Game Over

  66. Director Hovedmeny Level 1 ... n Game Over

  67. Fysikkmotor

  68. Fysikkmotor • Chipmunk

  69. Fysikkmotor • Chipmunk • Box2D

  70. None
  71. None
  72. Objective-C

  73. Objective-C Krasjkurs

  74. Objective-C Krasjkurs

  75. Objective-C Krasjkurs • C + Smalltalk

  76. Objective-C Krasjkurs • C + Smalltalk • Reference counting

  77. Objective-C Krasjkurs • C + Smalltalk • Reference counting •

    Xcode og llvm
  78. Metodekall Krasjkurs

  79. Metodekall Krasjkurs [objekt metodeNavn];

  80. Metodekall Krasjkurs [objekt metodeNavn];

  81. Metodekall Krasjkurs [objekt metodeNavn]; [objekt metodeNavn:param1];

  82. Metodekall Krasjkurs [objekt metodeNavn]; [objekt metodeNavn:param1]; [sprite showInLayer:layer1 x:100 y:200

    scaleBy:2.0f];
  83. Instansiering Krasjkurs

  84. Instansiering Krasjkurs Bird *b = [[Bird alloc] initWithSpeed:speed andSprite:sprite];

  85. Instansiering Krasjkurs Bird *b = [[Bird alloc] initWithSpeed:speed andSprite:sprite];

  86. Instansiering Krasjkurs Bird *b = [[Bird alloc] init];WithSpeed:speed andSprite:sprite];

  87. Instansiering Krasjkurs Bird *b = [[Bird alloc] initWithSpeed:speed andSprite:sprite];

  88. Instansiering Krasjkurs Bird *b = [[Bird alloc] initWithSpeed:speed andSprite:sprite]; [b

    release];
  89. Klasser Krasjkurs

  90. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  91. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  92. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  93. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  94. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  95. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  96. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  97. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  98. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  99. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; -(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  100. Klasser Krasjkurs @interface Bird : NSObject { @public CCSprite *sprite;

    float speed; } -(CCSprite*) getSprite; +(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  101. Implementasjon Krasjkurs

  102. Implementasjon Krasjkurs @implementation Bird -(CCSprite*) getSprite { return sprite; }

    @end
  103. Implementasjon Krasjkurs @implementation Bird -(CCSprite*) getSprite { return sprite; }

    @end
  104. Implementasjon Krasjkurs @implementation Bird -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s { self =

    [super init]; if ( self ) { speed = v; sprite = s; } return self; } @end
  105. Implementasjon Krasjkurs @implementation Bird -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s { self =

    [super init]; if ( self ) { speed = v; sprite = s; } return self; } @end
  106. Implementasjon Krasjkurs @implementation Bird -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s { self =

    [super init]; if ( self ) { speed = v; sprite = s; } return self; } @end
  107. Implementasjon Krasjkurs @implementation Bird -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s { self =

    [super init]; if ( self ) { speed = v; sprite = s; } return self; } @end
  108. BumblePop

  109. BumblePop

  110. Startup av app

  111. Startup av app iOS

  112. Startup av app - (void) applicationDidFinishLaunching:(UIApplication*)app iOS

  113. Startup av app - (void) applicationDidFinishLaunching:(UIApplication*)app

  114. Startup av app - (void) applicationDidFinishLaunching:(UIApplication*)app { window = [[UIWindow

    alloc] initWithFrame:/* ... */]; // ... CCDirector *director = [CCDirector sharedDirector]; [director setAnimationInterval:1.0/60]; [director setDisplayFPS:YES]; // ... [[CCDirector sharedDirector] runWithScene:[HelloWorldLayer scene]]; } - (void) applicationDidFinishLaunching:(UIApplication*)app
  115. Startup av app - (void) applicationDidFinishLaunching:(UIApplication*)app { window = [[UIWindow

    alloc] initWithFrame:/* ... */]; // ... CCDirector *director = [CCDirector sharedDirector]; [director setAnimationInterval:1.0/60]; [director setDisplayFPS:YES]; // ... [[CCDirector sharedDirector] runWithScene:[HelloWorldLayer scene]]; } - (void) applicationDidFinishLaunching:(UIApplication*)app
  116. Startup av layer

  117. Startup av layer -(id) init { if ( (self=[super init])

    ) { game = [[Game alloc] init]; [self schedule:@selector(nextFrame:)]; CCSprite *bee = [CCSprite spriteWithFile: @"BumbleBee40.png"]; [self addChild:bee]; // ... }
  118. Startup av layer -(id) init { if ( (self=[super init])

    ) { game = [[Game alloc] init]; [self schedule:@selector(nextFrame:)]; CCSprite *bee = [CCSprite spriteWithFile: @"BumbleBee40.png"]; [self addChild:bee]; // ... }
  119. Startup av layer -(id) init { if ( (self=[super init])

    ) { game = [[Game alloc] init]; [self schedule:@selector(nextFrame:)]; CCSprite *bee = [CCSprite spriteWithFile: @"BumbleBee40.png"]; [self addChild:bee]; // ... }
  120. Startup av layer -(id) init { if ( (self=[super init])

    ) { game = [[Game alloc] init]; [self schedule:@selector(nextFrame:)]; CCSprite *bee = [CCSprite spriteWithFile: @"BumbleBee40.png"]; [self addChild:bee]; // ... }
  121. Startup av layer CCLabelTTF *score; score = [CCLabelTTF labelWithString:@"Score: ?"

    fontName:@"Marker Felt" fontSize:22];
  122. Neste bilde -(void) draw { // Kun TEGNING, ingen oppdatering

    av state } - (void) nextFrame:(ccTime)dt { // Kun oppdatering av STATE }
  123. Bevegelse

  124. Bevegelse cloud.position = ccp( cloud.position.x - 10*dt, cloud.position.y);

  125. Bevegelse cloud.position = ccp( cloud.position.x - 10*dt, cloud.position.y); -10 px/sec

  126. Touch

  127. Touch - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint location =

    [self convertTouchToNodeSpace: touch]; beeDestination = location; }
  128. Målbevegelse

  129. Målbevegelse [self moveTowards:bee :&beeDestination :2*dt]; - (void) moveTowards: (CCSprite*)from: (CGPoint*)to:

    (float)amount { float dx = to->x - from.position.x; float dy = to->y - from.position.y; dx *= amount; dy *= amount; from.position = ccp(from.position.x + dx, from.position.y + dy); }
  130. Målbevegelse [self moveTowards:bee :&beeDestination :2*dt]; - (void) moveTowards: (CCSprite*)from: (CGPoint*)to:

    (float)amount { float dx = to->x - from.position.x; float dy = to->y - from.position.y; dx *= amount; dy *= amount; from.position = ccp(from.position.x + dx, from.position.y + dy); }
  131. Målbevegelse [self moveTowards:bee :&beeDestination :2*dt]; - (void) moveTowards: (CCSprite*)from: (CGPoint*)to:

    (float)amount { [from runAction: [CCMoveTo actionWithDuration:1.0f position:*to]]; }
  132. Fugler

  133. Fugler // Move ALL birds with their given speed for

    ( Bird *p in birds ) [self moveTowards:p->sprite :&beeDestination :p->speed*dt];
  134. Kollisjon

  135. Kollisjon -(void) beeHitBy:(CCSprite*)bird { CGRect beeBox = [bee boundingBox]; CGRect

    birdBox = [bird boundingBox]; if ( CGRectIntersectsRect(beeBox, birdBox) ) [self killBee]; }
  136. None
  137. None
  138. Oppsummering • iOS • Cocos2D + fysikkmotorer • Spill i

    Objective-C
  139. Takk!