From c9337218ef1660360097928c753bde1c79775618 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 23 Aug 2011 07:52:24 -0400 Subject: Added scrolling to level selection screen Using http://www.xcombinator.com/2010/09/08/a-paging-uiscrollview-in-cocos2d-with-previews/ as a base, I was able to implement a paging scroller for the level selection screen so players can swipe through available levels and choose one to play. At this point, the level selection screen is practically done--the only other thing I want to add is a UIPageControl to interact with the scrolling and give the player an indication of how many levels there are. Refs #207 --- Classes/GameModeSelectionLayer.m | 66 ++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 9 deletions(-) (limited to 'Classes/GameModeSelectionLayer.m') diff --git a/Classes/GameModeSelectionLayer.m b/Classes/GameModeSelectionLayer.m index a90606e..9875d41 100644 --- a/Classes/GameModeSelectionLayer.m +++ b/Classes/GameModeSelectionLayer.m @@ -11,6 +11,8 @@ #import #import "Cart_CollectAppDelegate.h" #import "MainMenuLayer.h" +#import "TutorialMode.h" +#import "ClassicGameMode.h" @implementation GameModeSelectionLayer @@ -36,12 +38,14 @@ if (nil != self) { - // Initialization code here. - GameModeSelection* tutorialSelection = [[[GameModeSelection alloc] initWithName:@"Tutorial" location:@"Florence" filename:@"florence" unlocked:YES] autorelease]; - tutorialSelection.position = ccp(160-32,160); - [self addChild:tutorialSelection]; - + gameModes = [[NSMutableArray alloc] init]; + CCMenu* menu = [CCMenu menuWithItems:nil]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + float onePanelWide = 128; + + GameModeSelection* tutorialSelection = [GameModeSelection selectionWithName:@"Tutorial" location:@"Florence" filename:@"florence" unlocked:YES]; + [gameModes addObject:tutorialSelection]; + GameModeSelection* collectSelection; if ([defaults boolForKey:@"hasDoneTutorial"]) @@ -58,13 +62,37 @@ } } - collectSelection = [[[GameModeSelection alloc] initWithName:@"Collect" location:@"Paris" filename:@"paris" highscore:score] autorelease]; + collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" highscore:score]; } else { - collectSelection = [[[GameModeSelection alloc] initWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"] autorelease]; + collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"]; + } + + [gameModes addObject:collectSelection]; + + float padding = 15; + float totalPanelWidth = onePanelWide + padding*2; + float numberOfPanels = [gameModes count]; + float totalWidth = numberOfPanels * totalPanelWidth; + int currentWorldOffset = [defaults integerForKey:@"lastSelectedMode"]; + CCLayer* panels = [CCLayer node]; + + for (GameModeSelection* gameMode in gameModes) + { + [gameMode setDelegate:self]; + [menu addChild:gameMode]; } - collectSelection.position = ccp(320+32,160); - [self addChild:collectSelection]; + [menu alignItemsHorizontallyWithPadding:padding*2]; + [panels addChild:menu]; + [self addChild:panels]; + + menu.position = ccpAdd(menu.position, ccp(totalWidth/2 - totalPanelWidth/2, 320)); + touchDelegatingView = [[TouchDelegatingView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)]; + scrollView = [[CocosOverlayScrollView alloc] initWithFrame:CGRectMake(0, 0, totalPanelWidth, 320) numPages:numberOfPanels width:totalPanelWidth layer:panels]; + touchDelegatingView.scrollView = scrollView; + [scrollView setContentOffset:CGPointMake(currentWorldOffset*totalPanelWidth+1,0) animated:NO]; + [[[CCDirector sharedDirector] openGLView] addSubview:touchDelegatingView]; + [[[CCDirector sharedDirector] openGLView] addSubview:scrollView]; CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)]; CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil]; @@ -75,9 +103,29 @@ return self; } +- (void)onExit +{ + [touchDelegatingView removeFromSuperview]; + [scrollView removeFromSuperview]; +} + - (void)mainmenu { [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; } +- (void)didSelectGameMode:(GameModeSelection *)gameMode +{ + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [defaults setInteger:[gameModes indexOfObject:gameMode] forKey:@"lastSelectedMode"]; + + if ([gameMode.name isEqual:@"Tutorial"]) + { + [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]]; + } else if ([gameMode.name isEqual:@"Collect"]) + { + [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]]; + } +} + @end -- cgit 1.4.1