Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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. http://www.cocos2d- iphone.org/wiki/doku.php/prog_guide:basic_conce pts

2 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 http://www.cocos2d- iphone.org/wiki/doku.php/prog_guide:lesson_3._m enus_and_scenes

3 What we have so far... Source files: AsteroidsAppDelegate.h/m implements UIApplicationDelegate AsteroidScene.h/m is-a CCLayer (the game layer), and implements CCTargetedTouchDelegate

4 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

5 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

6 Overlay layer interface[.h] @interface OverlayLayer : CCLayer { CCLabelAtlas *labelAtlas; } @property (readwrite,assign) CCLabelAtlas *labelAtlas; -(id) init; -(void) draw; @end

7 Overlay layer implementation[.m] @implementation OverlayLayer @synthesize labelAtlas; -(id) init { if( (self=[super init])) { // current game score label... labelAtlas = [CCLabelAtlas labelAtlasWithString:@"0000" charMapFile:@"fps_images.png" 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 stringWithFormat:@"%05d", asteroidCount, nil]; [labelAtlas setString:str]; } @end

8 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]; }

9 For a new scene Create the layer to be used by the new scene Write the call into the code

10 Translation to layer interface[.h] @interface YouLost : CCLayer { } -(id) init; @end

11 Translation layer implementation[.m] @implementation YouLost -(id) init { if( (self=[super init])) { CCLabel *label = [CCLabel labelWithString:@"Ooops... You hit an asteroid!" fontName:@"American Typewriter" fontSize:24]; [self addChild:label]; [label setPosition:ccp(170,20)]; } return self; } @end

12 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))) { NSLog(@"shipHitAsteroid!!!\n"); 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; }

13 Try some fancy scene translations http://www.cocos2d- 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]];


Download ppt "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."

Similar presentations


Ads by Google