From c55c499656ed089df63b049979487e9af9d109d6 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Mon, 15 Aug 2011 14:47:39 -0400 Subject: Fixed game over transition Fixes #189 --- Cart Collect.xcodeproj/project.pbxproj | 12 +- Classes/ClassicGameMode.m | 4 +- Classes/GameOverLayer.h | 27 ----- Classes/GameOverLayer.m | 199 --------------------------------- Classes/GameOverScene.h | 29 +++++ Classes/GameOverScene.m | 197 ++++++++++++++++++++++++++++++++ 6 files changed, 234 insertions(+), 234 deletions(-) delete mode 100755 Classes/GameOverLayer.h delete mode 100755 Classes/GameOverLayer.m create mode 100755 Classes/GameOverScene.h create mode 100755 Classes/GameOverScene.m diff --git a/Cart Collect.xcodeproj/project.pbxproj b/Cart Collect.xcodeproj/project.pbxproj index bd63c09..280cb2a 100755 --- a/Cart Collect.xcodeproj/project.pbxproj +++ b/Cart Collect.xcodeproj/project.pbxproj @@ -39,7 +39,7 @@ 3F6C7EC613D6335A00C038FE /* 1up.wav in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C7EC513D6335A00C038FE /* 1up.wav */; }; 3F6C7EE113D636BE00C038FE /* rock.png in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C7EE013D636BE00C038FE /* rock.png */; }; 3F6C7EE413D636CF00C038FE /* Rock.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C7EE313D636CF00C038FE /* Rock.m */; }; - 3F6C7EE913D6377D00C038FE /* GameOverLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C7EE813D6377D00C038FE /* GameOverLayer.m */; }; + 3F6C7EE913D6377D00C038FE /* GameOverScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C7EE813D6377D00C038FE /* GameOverScene.m */; }; 3F6C7F2413D63E6500C038FE /* SeaBeach.png in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C7F2313D63E6500C038FE /* SeaBeach.png */; }; 3F6C7F4B13D647A600C038FE /* GameOver.png in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C7F4A13D647A600C038FE /* GameOver.png */; }; 3F6C7F5B13D648CE00C038FE /* helvetica2.fnt in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C7F5913D648CE00C038FE /* helvetica2.fnt */; }; @@ -342,8 +342,8 @@ 3F6C7EE013D636BE00C038FE /* rock.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = rock.png; sourceTree = ""; }; 3F6C7EE213D636CF00C038FE /* Rock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Rock.h; sourceTree = ""; }; 3F6C7EE313D636CF00C038FE /* Rock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Rock.m; sourceTree = ""; }; - 3F6C7EE713D6377D00C038FE /* GameOverLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameOverLayer.h; sourceTree = ""; }; - 3F6C7EE813D6377D00C038FE /* GameOverLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameOverLayer.m; sourceTree = ""; }; + 3F6C7EE713D6377D00C038FE /* GameOverScene.h */ = {isa = PBXFileReference; fileEncoding = 4; path = GameOverScene.h; sourceTree = ""; }; + 3F6C7EE813D6377D00C038FE /* GameOverScene.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameOverScene.m; sourceTree = ""; }; 3F6C7F2313D63E6500C038FE /* SeaBeach.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = SeaBeach.png; sourceTree = ""; }; 3F6C7F4A13D647A600C038FE /* GameOver.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = GameOver.png; sourceTree = ""; }; 3F6C7F5913D648CE00C038FE /* helvetica2.fnt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = helvetica2.fnt; sourceTree = ""; }; @@ -702,8 +702,8 @@ E0F81037120A173C005866B8 /* RootViewController.m */, 1F3B9A2C0EF2145700286867 /* Cart_CollectAppDelegate.h */, 507022A2107672FA00393637 /* Cart_CollectAppDelegate.m */, - 3F6C7EE713D6377D00C038FE /* GameOverLayer.h */, - 3F6C7EE813D6377D00C038FE /* GameOverLayer.m */, + 3F6C7EE713D6377D00C038FE /* GameOverScene.h */, + 3F6C7EE813D6377D00C038FE /* GameOverScene.m */, 3F83955213D739B10059AEE8 /* MainMenuLayer.m */, 3F83955113D739B10059AEE8 /* MainMenuLayer.h */, 3F03221413D78F8C00E6A708 /* HighscoreListController.h */, @@ -1398,7 +1398,7 @@ 3F6C7C7013D5E54E00C038FE /* Bottle.m in Sources */, 3F6C7C7313D5E57100C038FE /* OneUp.m in Sources */, 3F6C7EE413D636CF00C038FE /* Rock.m in Sources */, - 3F6C7EE913D6377D00C038FE /* GameOverLayer.m in Sources */, + 3F6C7EE913D6377D00C038FE /* GameOverScene.m in Sources */, 3F83955313D739B10059AEE8 /* MainMenuLayer.m in Sources */, 3F03221613D78F8C00E6A708 /* HighscoreListController.m in Sources */, 3F03221913D7904E00E6A708 /* Highscore.m in Sources */, diff --git a/Classes/ClassicGameMode.m b/Classes/ClassicGameMode.m index eb766a1..88c52ce 100755 --- a/Classes/ClassicGameMode.m +++ b/Classes/ClassicGameMode.m @@ -12,7 +12,7 @@ #import "Bottle.h" #import "OneUp.h" #import "Rock.h" -#import "GameOverLayer.h" +#import "GameOverScene.h" #import "SimpleAudioEngine.h" @implementation ClassicGameMode @@ -27,7 +27,7 @@ { [self unscheduleAllSelectors]; - [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]]; + [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverScene sceneWithScore:score]]]; } else if (score > lastScore) { if ((lastScore < 6500) && (score >= 6500)) diff --git a/Classes/GameOverLayer.h b/Classes/GameOverLayer.h deleted file mode 100755 index c800e82..0000000 --- a/Classes/GameOverLayer.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// GameOverLayer.h -// Cart Collect -// -// Created by iD Student Account on 7/19/11. -// Copyright 2011 __MyCompanyName__. All rights reserved. -// - -#import -#import "cocos2d.h" - -@interface GameOverLayer : CCLayer { - UILabel* scoreField; - UITextField* textField; - UISwitch* submitSwitch; - UIActivityIndicatorView* activityIndicator; - UIButton* backButton; - int score; -} - -+ (CCScene*)sceneWithScore:(int)score; -- (id)initWithScore:(int)score; -- (void)newgame; -- (void)submitScore; -- (void)exit; - -@end diff --git a/Classes/GameOverLayer.m b/Classes/GameOverLayer.m deleted file mode 100755 index 9f58f01..0000000 --- a/Classes/GameOverLayer.m +++ /dev/null @@ -1,199 +0,0 @@ -// -// GameOverLayer.m -// Cart Collect -// -// Created by iD Student Account on 7/19/11. -// Copyright 2011 __MyCompanyName__. All rights reserved. -// - -#import "GameOverLayer.h" -#import "Cart_CollectAppDelegate.h" -#import -#import "cocoslive.h" -#import "MainMenuLayer.h" - -@implementation GameOverLayer - -+ (CCScene*)sceneWithScore:(int)score -{ - CCScene* scene = [CCScene node]; - - CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; - [scene addChild:backgroundLayer]; - - GameOverLayer* layer = [[[GameOverLayer alloc] initWithScore:score] autorelease]; - [scene addChild:layer]; - - return scene; -} - -- (id)initWithScore:(int)score2 -{ - self = [super init]; - - if (nil != self) - { - CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"]; - backgroundImage.position = ccp(240, 160); - [self addChild:backgroundImage z:0]; - - score = score2; - - scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200-320, 0, 0)]; - [scoreField setFont:[UIFont systemFontOfSize:20.0f]]; - [scoreField setBackgroundColor:[UIColor clearColor]]; - [scoreField setText:[NSString stringWithFormat:@"%d", score2]]; - CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip]; - [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)]; - [[[CCDirector sharedDirector] openGLView] addSubview:scoreField]; - - textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247-320, 216, 31)]; - [textField setFont:[UIFont systemFontOfSize:20.0f]]; - [textField setBackgroundColor:[UIColor clearColor]]; - [textField setDelegate:self]; - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - [textField setText:[defaults objectForKey:@"username"]]; - [[[CCDirector sharedDirector] openGLView] addSubview:textField]; - - submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161-320, 0, 0)]; - [submitSwitch setOn:[defaults boolForKey:@"submitScore"]]; - [[[CCDirector sharedDirector] openGLView] addSubview:submitSwitch]; - - activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)]; - [[[CCDirector sharedDirector] openGLView] addSubview:activityIndicator]; - - backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; - [backButton setFrame:CGRectMake(240-154/2, 320-60-15-320, 154, 31)]; - [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal]; - [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside]; - [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]]; - [backButton.titleLabel setTextColor:[UIColor blackColor]]; - - [[[CCDirector sharedDirector] openGLView] addSubview:backButton]; - } - - return self; -} - -- (void)onEnter -{ - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationDuration:1.5f]; - [UIView setAnimationCurve:UIViewAnimationCurveLinear]; - scoreField.transform = CGAffineTransformMakeTranslation(0, 320); - textField.transform = CGAffineTransformMakeTranslation(0, 320); - submitSwitch.transform = CGAffineTransformMakeTranslation(0, 320); - backButton.transform = CGAffineTransformMakeTranslation(0, 320); - [UIView commitAnimations]; -} - -- (void)newgame -{ - backButton.enabled = NO; - textField.enabled = NO; - submitSwitch.enabled = NO; - - const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String]; - sqlite3_stmt* compiled_statement; - - if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) - { - sqlite3_step(compiled_statement); - NSLog(@"awesome, %@", [textField text]); - } else { - NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database])); - } - - if (submitSwitch.on) - { - [self submitScore]; - } else { - [self exit]; - } -} - -- (void)submitScore -{ - [activityIndicator startAnimating]; - - CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self]; - NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; - [dict setObject:@"Classic" forKey:@"cc_category"]; - [dict setObject:[textField text] forKey:@"cc_playername"]; - [dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"]; - [server sendScore:dict]; - [server release]; - [dict release]; -} - -- (BOOL)textFieldShouldReturn:(UITextField *)m_textField -{ - [m_textField resignFirstResponder]; - - return YES; -} - -- (void)scorePostOk:(id)sender -{ - [activityIndicator stopAnimating]; - - // Score post successful - [self exit]; -} - -- (void)scorePostFail:(id)sender -{ - [activityIndicator stopAnimating]; - - // score post failed - tPostStatus status = [sender postStatus]; - if( status == kPostStatusPostFailed ) { - NSLog(@"SERVER ERROR"); - // an error with the server ? - // try again - }else if( status == kPostStatusConnectionFailed ) { - NSLog(@"CONNECTION FAILURE"); - // a error establishing the connection ? - // turn-on wifi, and then try again - } - - UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Whoops" message:@"There was an error posting your score. Please make sure you have Internet access." delegate:self cancelButtonTitle:@"Try Again" otherButtonTitles:@"Cancel", nil]; - [alertView show]; - [alertView release]; -} - -- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex -{ - if (buttonIndex == 0) - { - [self submitScore]; - } else if (buttonIndex == 1) - { - [self exit]; - } -} - -- (void)exit -{ - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - [defaults setObject:[textField text] forKey:@"username"]; - [defaults setBool:submitSwitch.on forKey:@"submitScore"]; - - [submitSwitch removeFromSuperview]; - [submitSwitch release]; - - [textField removeFromSuperview]; - [textField release]; - - [scoreField removeFromSuperview]; - [scoreField release]; - - [activityIndicator removeFromSuperview]; - [activityIndicator release]; - - [backButton removeFromSuperview]; - - [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; -} - -@end diff --git a/Classes/GameOverScene.h b/Classes/GameOverScene.h new file mode 100755 index 0000000..ef63aa7 --- /dev/null +++ b/Classes/GameOverScene.h @@ -0,0 +1,29 @@ +// +// GameOverLayer.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "cocos2d.h" + +@interface GameOverScene : CCScene { + CCLayer* theLayer; + UIView* movingLayer; + UILabel* scoreField; + UITextField* textField; + UISwitch* submitSwitch; + UIActivityIndicatorView* activityIndicator; + UIButton* backButton; + int score; +} + ++ (GameOverScene*)sceneWithScore:(int)score; +- (id)initWithScore:(int)score; +- (void)newgame; +- (void)submitScore; +- (void)exit; + +@end diff --git a/Classes/GameOverScene.m b/Classes/GameOverScene.m new file mode 100755 index 0000000..bc081ff --- /dev/null +++ b/Classes/GameOverScene.m @@ -0,0 +1,197 @@ +// +// GameOverLayer.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "GameOverScene.h" +#import "Cart_CollectAppDelegate.h" +#import +#import "cocoslive.h" +#import "MainMenuLayer.h" + +@implementation GameOverScene + ++ (GameOverScene*)sceneWithScore:(int)score +{ + return [[[GameOverScene alloc] initWithScore:score] autorelease]; +} + +- (id)initWithScore:(int)score2 +{ + self = [super init]; + + if (nil != self) + { + CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; + [self addChild:backgroundLayer]; + + theLayer = [CCLayer node]; + [self addChild:theLayer]; + + CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"]; + backgroundImage.position = ccp(240, 160); + [theLayer addChild:backgroundImage z:0]; + + score = score2; + + movingLayer = [[UIView alloc] initWithFrame:CGRectMake(0, -320, 480, 320)]; + movingLayer.backgroundColor = [UIColor clearColor]; + [[[CCDirector sharedDirector] openGLView] addSubview:movingLayer]; + + scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200, 0, 0)]; + [scoreField setFont:[UIFont systemFontOfSize:20.0f]]; + [scoreField setBackgroundColor:[UIColor clearColor]]; + [scoreField setText:[NSString stringWithFormat:@"%d", score2]]; + CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip]; + [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)]; + [movingLayer addSubview:scoreField]; + + textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247, 216, 31)]; + [textField setFont:[UIFont systemFontOfSize:20.0f]]; + [textField setBackgroundColor:[UIColor clearColor]]; + [textField setDelegate:self]; + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [textField setText:[defaults objectForKey:@"username"]]; + [movingLayer addSubview:textField]; + + submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161, 0, 0)]; + [submitSwitch setOn:[defaults boolForKey:@"submitScore"]]; + [movingLayer addSubview:submitSwitch]; + + activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)]; + [movingLayer addSubview:activityIndicator]; + + backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; + [backButton setFrame:CGRectMake(240-154/2, 320-60-15, 154, 31)]; + [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal]; + [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside]; + [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]]; + [backButton.titleLabel setTextColor:[UIColor blackColor]]; + [movingLayer addSubview:backButton]; + } + + return self; +} + +- (void)setPosition:(CGPoint)position +{ + [super setPosition:position]; + movingLayer.center = CGPointMake(240, 160-position.y); +} + +- (void)newgame +{ + backButton.enabled = NO; + textField.enabled = NO; + submitSwitch.enabled = NO; + + const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String]; + sqlite3_stmt* compiled_statement; + + if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) + { + sqlite3_step(compiled_statement); + NSLog(@"awesome, %@", [textField text]); + } else { + NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database])); + } + + if (submitSwitch.on) + { + [self submitScore]; + } else { + [self exit]; + } +} + +- (void)submitScore +{ + [activityIndicator startAnimating]; + + CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self]; + NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; + [dict setObject:@"Classic" forKey:@"cc_category"]; + [dict setObject:[textField text] forKey:@"cc_playername"]; + [dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"]; + [server sendScore:dict]; + [server release]; + [dict release]; +} + +- (BOOL)textFieldShouldReturn:(UITextField *)m_textField +{ + [m_textField resignFirstResponder]; + + return YES; +} + +- (void)scorePostOk:(id)sender +{ + [activityIndicator stopAnimating]; + + // Score post successful + [self exit]; +} + +- (void)scorePostFail:(id)sender +{ + [activityIndicator stopAnimating]; + + // score post failed + tPostStatus status = [sender postStatus]; + if( status == kPostStatusPostFailed ) { + NSLog(@"SERVER ERROR"); + // an error with the server ? + // try again + }else if( status == kPostStatusConnectionFailed ) { + NSLog(@"CONNECTION FAILURE"); + // a error establishing the connection ? + // turn-on wifi, and then try again + } + + UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Whoops" message:@"There was an error posting your score. Please make sure you have Internet access." delegate:self cancelButtonTitle:@"Try Again" otherButtonTitles:@"Cancel", nil]; + [alertView show]; + [alertView release]; +} + +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex +{ + if (buttonIndex == 0) + { + [self submitScore]; + } else if (buttonIndex == 1) + { + [self exit]; + } +} + +- (void)exit +{ + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [defaults setObject:[textField text] forKey:@"username"]; + [defaults setBool:submitSwitch.on forKey:@"submitScore"]; + + [submitSwitch removeFromSuperview]; + [submitSwitch release]; + + [textField removeFromSuperview]; + [textField release]; + + [scoreField removeFromSuperview]; + [scoreField release]; + + [activityIndicator removeFromSuperview]; + [activityIndicator release]; + + [backButton removeFromSuperview]; + + [movingLayer removeFromSuperview]; + [movingLayer release]; + + [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; +} + +@end -- cgit 1.4.1