Simple Scene Management a layer has the size of the device screen it defines appearance and behavior a scene is composed of one or more layers app transitions take place through scenes the director takes care of transitioning between scenes. iphone.org/wiki/doku.php/prog_guide:basic_conce pts
Simple Scene Management typically one scene is used for the “playable” part of the game others scenes for: title, high scores, options runWithScene tells director to use that scene replaceScene replaces the running scene with another pauseScene puts the current scene on hold for another iphone.org/wiki/doku.php/prog_guide:lesson_3._m enus_and_scenes
What we have so far... Source files: AsteroidsAppDelegate.h/m implements UIApplicationDelegate AsteroidScene.h/m is-a CCLayer (the game layer), and implements CCTargetedTouchDelegate
What we will add... A layer over the game layer showing game statistics A new scene to go to when the ship hits an asteroid
For an Overlay Layer add a layer (overlay layer) on top of the game layer containing game status add a scene that uses the overlay layer and the game layer
Overlay layer OverlayLayer : CCLayer { CCLabelAtlas *labelAtlas; (readwrite,assign) CCLabelAtlas *labelAtlas; -(id) init; -(void)
Overlay layer labelAtlas; -(id) init { if( (self=[super init])) { // current game score label... labelAtlas = [CCLabelAtlas itemWidth:16 itemHeight:24 startCharMap:'.']; [self addChild:labelAtlas]; [label setAnchorPoint:ccp(0,0.5f)]; // left align, the default is center align [label setPosition:ccp(20,20)]; } return self; } - (void) draw { [labelAtlas setVisible: YES]; // Update the score on the screen.... NSString *str = [NSString asteroidCount, nil]; [labelAtlas setString:str];
Modify application delegate to use it - (void) applicationDidFinishLaunching:(UIApplication*)application { CC_DIRECTOR_INIT(); // Obtain the shared director in order to... CCDirector *director = [CCDirector sharedDirector]; // Sets landscape mode [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; // Whether or not to display the Frames/Second (FPS) on the bottom-left corner //[director setDisplayFPS:YES]; // UIView subclass for rendering OpenGL scene into. EAGLView *view = [director openGLView]; // Turn on multiple touches [view setMultipleTouchEnabled:YES]; //[director runWithScene: [AsteroidsScene scene]]; CCScene *scene = [CCScene node]; [scene addChild: [AsteroidsScene node] z:0]; // 'z' gives the order stacking [scene addChild: [OverlayLayer node] z:1]; [director runWithScene: scene]; }
For a new scene Create the layer to be used by the new scene Write the call into the code
Translation to layer YouLost : CCLayer { } -(id)
Translation layer YouLost -(id) init { if( (self=[super init])) { CCLabel *label = [CCLabel You hit an asteroid!" Typewriter" fontSize:24]; [self addChild:label]; [label setPosition:ccp(170,20)]; } return self;
Write the call into the code //// //// Chipmunk Physics colission callbacks... //// // Begin: Two shapes just started touching for the first time this step. Return true from the callback to process the collision // normally or false to cause Chipmunk to ignore the collision entirely. If you return false, the pre-solve and post-solve // callbacks will never be run, but you will still recieve a separate event when the shapes stop overlapping. int beginShipHitAsteroid(cpArbiter *arb, cpSpace *space __attribute__ ((unused)), void *data __attribute__ ((unused))) { CCScene *scene = [[CCScene node] addChild:[YouLost node] z:0]; [[CCDirector sharedDirector] replaceScene: scene]; // Returning 0 will cause the collision to be discarded. This allows you to do conditional collisions. return 1; }
Try some fancy scene translations iphone.org/wiki/doku.php/prog_guide:lesson_3._m enus_and_scenes Try some fancy transitions: CCFadeTransition CCFlipAngularTransition CCShrinkGrowTransition CCMoveInBTransition CCMoveInTTransition CCMoveInLTransition CCMoveInRTransition CCFadeTRTransition CCFadeUpTransition CCFlipXTransition CCFlipYTransition CCPageTurnTransition CCCrossFadeTransition [[CCDirector sharedDirector] replaceScene: [CCFadeTransition transitionWithDuration:3.0f scene:scene]];