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/CocosOverlayScrollView.h | 18 +++++++ Classes/CocosOverlayScrollView.m | 99 +++++++++++++++++++++++++++++++++++++ Classes/GameModeSelection.h | 8 ++- Classes/GameModeSelection.m | 35 +++++++++---- Classes/GameModeSelectionDelegate.h | 17 +++++++ Classes/GameModeSelectionLayer.h | 9 +++- Classes/GameModeSelectionLayer.m | 66 +++++++++++++++++++++---- Classes/NMPanelMenu.h | 14 ++++++ Classes/NMPanelMenu.m | 37 ++++++++++++++ Classes/TouchDelegatingView.h | 18 +++++++ Classes/TouchDelegatingView.m | 26 ++++++++++ 11 files changed, 326 insertions(+), 21 deletions(-) create mode 100755 Classes/CocosOverlayScrollView.h create mode 100755 Classes/CocosOverlayScrollView.m create mode 100644 Classes/GameModeSelectionDelegate.h create mode 100755 Classes/NMPanelMenu.h create mode 100755 Classes/NMPanelMenu.m create mode 100755 Classes/TouchDelegatingView.h create mode 100755 Classes/TouchDelegatingView.m (limited to 'Classes') diff --git a/Classes/CocosOverlayScrollView.h b/Classes/CocosOverlayScrollView.h new file mode 100755 index 0000000..29de1c7 --- /dev/null +++ b/Classes/CocosOverlayScrollView.h @@ -0,0 +1,18 @@ +// +// CocosOverlayScrollView.h +// shapes +// +// Created by Nate Murray on 8/23/10. +// Copyright 2010 LittleHiccup. All rights reserved. +// + +#import +#import "cocos2d.h" + +@interface CocosOverlayScrollView : UIScrollView +{ + CCNode* targetLayer; +} +@property(nonatomic, retain) CCNode* targetLayer; +-(id) initWithFrame: (CGRect) frameRect numPages: (int) numPages width: (float) width layer: (CCNode*) layer; +@end diff --git a/Classes/CocosOverlayScrollView.m b/Classes/CocosOverlayScrollView.m new file mode 100755 index 0000000..dcf5571 --- /dev/null +++ b/Classes/CocosOverlayScrollView.m @@ -0,0 +1,99 @@ +// +// CocosOverlayScrollView.m +// shapes +// +// Created by Nate Murray on 8/23/10. +// Copyright 2010 LittleHiccup. All rights reserved. +// + +#import "CocosOverlayScrollView.h" + +@implementation CocosOverlayScrollView +@synthesize targetLayer; + +// Configure your favorite UIScrollView options here +-(id) initWithFrame: (CGRect) frameRect numPages: (int) numPages width: (float) width layer: (CCNode*) layer { + if ((self = [super initWithFrame: frameRect])){ + self.contentSize = CGSizeMake(width*numPages, 320); + self.bounces = YES; + self.delaysContentTouches = NO; + self.delegate = self; + self.pagingEnabled = YES; + self.scrollsToTop = NO; + self.showsVerticalScrollIndicator = NO; + self.showsHorizontalScrollIndicator = NO; + [self setUserInteractionEnabled:TRUE]; + [self setScrollEnabled:TRUE]; + self.targetLayer = layer; + // self.canCancelContentTouches = YES; + } + return self; +} + +-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event +{ + if (!self.dragging) + { + // UITouch* touch = [[touches allObjects] objectAtIndex:0]; + // CGPoint location = [touch locationInView: [[touch view] superview]]; + // CCLOG(@"touch at l.x:%f l.y:%f", location.x, location.y); + + [self.nextResponder touchesBegan: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesBegan:touches withEvent:event]; + } + + [super touchesBegan: touches withEvent: event]; +} + +-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event +{ + if (!self.dragging) + { + [self.nextResponder touchesEnded: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesEnded:touches withEvent:event]; + } + + [super touchesEnded: touches withEvent: event]; +} + +-(void) touchesCancelled: (NSSet *) touches withEvent: (UIEvent *) event +{ + // if (!self.dragging) + // { + // CCLOG(@"CocosOverlayScrollView touchesEnded not dragging"); + [self.nextResponder touchesCancelled: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesCancelled:touches withEvent:event]; + // } + [super touchesCancelled: touches withEvent: event]; +} + + +- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView +{ + // TODO - Custom code for handling deceleration of the scroll view +} + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView +{ + CGPoint dragPt = [scrollView contentOffset]; + dragPt = [[CCDirector sharedDirector] convertToGL:dragPt]; + + dragPt.y = dragPt.y * -1; + dragPt.x = dragPt.x * -1; + + CGPoint newLayerPosition = CGPointMake(dragPt.x, dragPt.y); + + [targetLayer setPosition:newLayerPosition]; +} + +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView +{ + // CGPoint dragPt = [scrollView contentOffset]; + // etc. +} + +-(void) dealloc { + self.targetLayer = nil; + [super dealloc]; +} +@end \ No newline at end of file diff --git a/Classes/GameModeSelection.h b/Classes/GameModeSelection.h index f6aaeff..f69ea37 100644 --- a/Classes/GameModeSelection.h +++ b/Classes/GameModeSelection.h @@ -7,17 +7,23 @@ // #import "cocos2d.h" +#import "GameModeSelectionDelegate.h" -@interface GameModeSelection : CCNode { +@interface GameModeSelection : CCMenuItem { NSString* name; NSString* location; BOOL unlocked; NSString* unlockCondition; + id delegate; } @property (readonly) NSString* name; @property (readonly) NSString* location; @property (readonly) BOOL unlocked; +@property (nonatomic,retain) id delegate; ++ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked; ++ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore; ++ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition; - (id)initWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked; - (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore; - (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition; diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m index fc6797a..582c9dd 100644 --- a/Classes/GameModeSelection.m +++ b/Classes/GameModeSelection.m @@ -7,17 +7,31 @@ // #import "GameModeSelection.h" -#import "TutorialMode.h" -#import "ClassicGameMode.h" #import "UIImage+ColorMasking.h" +#import "NMPanelMenu.h" @implementation GameModeSelection -@synthesize name, location, unlocked; +@synthesize name, location, unlocked, delegate; + ++ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked +{ + return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease]; +} + ++ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore +{ + return [[[GameModeSelection alloc] initWithName:name location:location filename:filename highscore:highscore] autorelease]; +} + ++ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition +{ + return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease]; +} - (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked; { - self = [super init]; + self = [super initWithTarget:nil selector:nil]; if (nil != self) { @@ -27,6 +41,8 @@ location = m_location; unlocked = m_unlocked; + contentSize_ = CGSizeMake(128, 320); + NSString* filenameMod; if (unlocked) @@ -124,7 +140,7 @@ CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]]; CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)]; - CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil]; + NMPanelMenu* theMenu = [NMPanelMenu menuWithItems:pictureMenuItem, nil]; theMenu.position = ccp(-5, 0); [self addChild:theMenu]; @@ -196,12 +212,11 @@ { if (unlocked) { - if ([name isEqual:@"Tutorial"]) - { - [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]]; - } else if ([name isEqual:@"Collect"]) + if (delegate != nil) { - [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]]; + [delegate didSelectGameMode:self]; + } else { + NSLog(@"I don't have a GameModeSelectionDelegate to call for some reason..."); } } else { UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; diff --git a/Classes/GameModeSelectionDelegate.h b/Classes/GameModeSelectionDelegate.h new file mode 100644 index 0000000..854a980 --- /dev/null +++ b/Classes/GameModeSelectionDelegate.h @@ -0,0 +1,17 @@ +// +// GameModeSelectionDelegate.h +// Cartographic +// +// Created by Starla Insigna on 8/23/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import + +@class GameModeSelection; + +@protocol GameModeSelectionDelegate + +- (void)didSelectGameMode:(GameModeSelection*)gameMode; + +@end diff --git a/Classes/GameModeSelectionLayer.h b/Classes/GameModeSelectionLayer.h index 2f57bfb..451a5f8 100644 --- a/Classes/GameModeSelectionLayer.h +++ b/Classes/GameModeSelectionLayer.h @@ -7,8 +7,15 @@ // #import "cocos2d.h" +#import "TouchDelegatingView.h" +#import "CocosOverlayScrollView.h" +#import "GameModeSelectionDelegate.h" -@interface GameModeSelectionLayer : CCLayer +@interface GameModeSelectionLayer : CCLayer { + NSMutableArray* gameModes; + TouchDelegatingView* touchDelegatingView; + CocosOverlayScrollView* scrollView; +} + (CCScene*)scene; - (id)init; 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 diff --git a/Classes/NMPanelMenu.h b/Classes/NMPanelMenu.h new file mode 100755 index 0000000..3487849 --- /dev/null +++ b/Classes/NMPanelMenu.h @@ -0,0 +1,14 @@ +/* + * NMPanelMenu.h + * shapes + * + * Created by Nate Murray on 7/29/10. + * Copyright 2010 YetiApps. All rights reserved. + * + */ + +#import "cocos2d.h" +@interface NMPanelMenu : CCMenu { +} +-(CCMenuItem *) itemForTouch: (UITouch *) touch; +@end diff --git a/Classes/NMPanelMenu.m b/Classes/NMPanelMenu.m new file mode 100755 index 0000000..ee24279 --- /dev/null +++ b/Classes/NMPanelMenu.m @@ -0,0 +1,37 @@ +/* + * NMPanelMenu.m + * shapes + * + * Created by Nate Murray on 7/29/10. + * Copyright 2010 YetiApps. All rights reserved. + * + */ + +#import "NMPanelMenu.h" + +@implementation NMPanelMenu + +-(CCMenuItem *) itemForTouch: (UITouch *) touch +{ + CGPoint touchLocation = [touch locationInView: [[touch view] superview]]; + touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; + + CCMenuItem* item; + CCARRAY_FOREACH(children_, item){ + // ignore invisible and disabled items: issue #779, #866 + if ( [item visible] && [item isEnabled] ) { + + CGPoint local = [item convertToNodeSpace:touchLocation]; + + CGRect r = [item rect]; + r.origin = CGPointZero; + + if( CGRectContainsPoint( r, local ) ) { + return item; + } + } + } + return nil; +} + +@end diff --git a/Classes/TouchDelegatingView.h b/Classes/TouchDelegatingView.h new file mode 100755 index 0000000..5ed6884 --- /dev/null +++ b/Classes/TouchDelegatingView.h @@ -0,0 +1,18 @@ +// +// TouchDelegatingView.h +// shapes +// +// Created by Nate Murray on 8/23/10. +// Copyright 2010 LittleHiccup. All rights reserved. +// + +#import +#import "CocosOverLayScrollView.h" + +@interface TouchDelegatingView : UIView { + // UIPageControl* pageControl; + CocosOverlayScrollView* scrollView; +} +@property(nonatomic, retain) CocosOverlayScrollView* scrollView; + +@end diff --git a/Classes/TouchDelegatingView.m b/Classes/TouchDelegatingView.m new file mode 100755 index 0000000..83bda73 --- /dev/null +++ b/Classes/TouchDelegatingView.m @@ -0,0 +1,26 @@ +// +// TouchDelegatingView.m +// Jacob's Shapes +// +// Created by Nate Murray on 8/23/10. +// Copyright 2010 LittleHiccup. All rights reserved. +// + +#import "TouchDelegatingView.h" + +@implementation TouchDelegatingView +@synthesize scrollView; + +- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { + if ([self pointInside:point withEvent:event]) { + return self.scrollView; + } + return nil; +} + +-(void) dealloc { + self.scrollView = nil; + [super dealloc]; +} + +@end -- cgit 1.4.1