Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iPhone game programming with Cocos2D

Christian Stigen Larsen
October 11, 2011
67

iPhone game programming with Cocos2D

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

Christian Stigen Larsen

October 11, 2011
Tweet

Transcript

  1. iOS

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

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

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

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

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

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

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

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

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

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

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

    float speed; } -(CCSprite*) getSprite; +(float) getSpeed; -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s; @end
  13. Implementasjon Krasjkurs @implementation Bird -(Bird*) initWithSpeed:(float)v andSprite:(CCSprite*)s { self =

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

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

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

    [super init]; if ( self ) { speed = v; sprite = s; } return self; } @end
  17. 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
  18. 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
  19. 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]; // ... }
  20. 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]; // ... }
  21. 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]; // ... }
  22. 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]; // ... }
  23. Neste bilde -(void) draw { // Kun TEGNING, ingen oppdatering

    av state } - (void) nextFrame:(ccTime)dt { // Kun oppdatering av STATE }
  24. Touch - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint location =

    [self convertTouchToNodeSpace: touch]; beeDestination = location; }
  25. 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); }
  26. 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); }
  27. Målbevegelse [self moveTowards:bee :&beeDestination :2*dt]; - (void) moveTowards: (CCSprite*)from: (CGPoint*)to:

    (float)amount { [from runAction: [CCMoveTo actionWithDuration:1.0f position:*to]]; }
  28. Fugler // Move ALL birds with their given speed for

    ( Bird *p in birds ) [self moveTowards:p->sprite :&beeDestination :p->speed*dt];
  29. Kollisjon -(void) beeHitBy:(CCSprite*)bird { CGRect beeBox = [bee boundingBox]; CGRect

    birdBox = [bird boundingBox]; if ( CGRectIntersectsRect(beeBox, birdBox) ) [self killBee]; }