From 8123ad65be46e9b7243d3429dd083638eece0127 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Fri, 4 Jan 2013 15:02:20 -0500 Subject: Made pausing easier You can now pause by tapping in the top half of the screen in any mode that supports pausing. Closes #199 --- Classes/GameMode.h | 10 ++++++--- Classes/GameMode.m | 60 +++++++++++++++++++++++++++++++++++++++++++------- Classes/JumpGameMode.h | 4 +--- Classes/JumpGameMode.m | 19 ++-------------- 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/Classes/GameMode.h b/Classes/GameMode.h index e208d49..ae3fbe5 100644 --- a/Classes/GameMode.h +++ b/Classes/GameMode.h @@ -15,7 +15,7 @@ #define GAME_SCENE 436 #define GAME_LAYER 437 -@interface GameMode : CCLayer { +@interface GameMode : CCLayer { NSMutableSet* objects; int score; int lives; @@ -23,19 +23,23 @@ Cart* cart; FallingObjectFactory* objectFactory; + CCLayerColor* willPauseLayer; CCLayerColor* shadedLayer; CCLayer* pauseLayer; + BOOL isPaused; + BOOL isPausing; CCLabelBMFont* scoreLabel; CCLabelBMFont* livesLabel; void (^delayedAction)(void); - BOOL isPaused; - BOOL hasGyroscope; double pitch; CMMotionManager* motionManager; + + BOOL isGesturing; + CGPoint gestureStartPoint; } @property (readonly) Cart* cart; diff --git a/Classes/GameMode.m b/Classes/GameMode.m index 49d0053..ca269cd 100644 --- a/Classes/GameMode.m +++ b/Classes/GameMode.m @@ -42,14 +42,6 @@ objectFactory = [[FallingObjectFactory alloc] init]; - if ([self canPause]) - { - CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)]; - CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; - [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; - [self addChild:pauseMenu]; - } - isPaused = NO; Class cmClass = (NSClassFromString(@"CMMotionManager")); @@ -71,6 +63,8 @@ isAccelerometerEnabled_ = YES; hasGyroscope = NO; } + + self.isTouchEnabled = YES; } return self; @@ -262,6 +256,56 @@ [cart deviceDidRotate:pitch]; } +- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event +{ + if ((!isPaused) && (!isGesturing) && ([self canPause])) + { + UITouch* touch = [touches anyObject]; + gestureStartPoint = [touch locationInView:nil]; + isGesturing = YES; + + if (gestureStartPoint.x > 160) + { + isPausing = YES; + + willPauseLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 63) width:480 height:160]; + willPauseLayer.position = ccp(0, 160); + [[[CCDirector sharedDirector] runningScene] addChild:willPauseLayer]; + } + } +} + +- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event +{ + if (isGesturing && isPausing) + { + UITouch* touch = [touches anyObject]; + CGPoint gestureCurrentPosition = [touch locationInView:nil]; + CGFloat distance = sqrt(powf((gestureCurrentPosition.x - gestureStartPoint.x),2) + powf((gestureCurrentPosition.y - gestureStartPoint.y),2)); + + if (distance > 2) + { + [[[CCDirector sharedDirector] runningScene] removeChild:willPauseLayer cleanup:YES]; + willPauseLayer = nil; + isPausing = NO; + } + } +} + +- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +{ + if (isPausing) + { + [[[CCDirector sharedDirector] runningScene] removeChild:willPauseLayer cleanup:YES]; + willPauseLayer = nil; + isPausing = NO; + + [self pause]; + } + + isGesturing = NO; +} + - (void)dealloc { [objects release]; diff --git a/Classes/JumpGameMode.h b/Classes/JumpGameMode.h index b18fd5a..a09bbea 100644 --- a/Classes/JumpGameMode.h +++ b/Classes/JumpGameMode.h @@ -11,12 +11,10 @@ @class LedgeFactory; -@interface JumpGameMode : GameMode { +@interface JumpGameMode : GameMode { CCSprite* water; int waterTick; BOOL wave; - BOOL isGesturing; - CGPoint gestureStartPoint; int jumpTick; BOOL jump; float expectedAngle; diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m index f3e690c..f0cb469 100644 --- a/Classes/JumpGameMode.m +++ b/Classes/JumpGameMode.m @@ -83,8 +83,6 @@ static GameModeInfo* info; cart.falling = YES; cart.delegate = self; - self.isTouchEnabled = YES; - waterTick = 0; wave = NO; @@ -279,18 +277,10 @@ static GameModeInfo* info; } } -- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -{ - if ((!isPaused) && (!isGesturing)) - { - UITouch* touch = [touches anyObject]; - gestureStartPoint = [touch locationInView:nil]; - isGesturing = YES; - } -} - - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + [super ccTouchesMoved:touches withEvent:event]; + if (isGesturing) { UITouch* touch = [touches anyObject]; @@ -306,11 +296,6 @@ static GameModeInfo* info; } } -- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event -{ - isGesturing = NO; -} - - (int)cartShouldFall:(Cart *)m_cart { int bottom = 0-m_cart.sprite.boundingBox.size.height/2; -- cgit 1.4.1