summary refs log tree commit diff stats
path: root/Classes/GameOverLayer.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-08-15 14:47:39 -0400
committerStarla Insigna <starla4444@gmail.com>2011-08-15 14:47:39 -0400
commitc55c499656ed089df63b049979487e9af9d109d6 (patch)
treea07f86c4630ad35e10304c50c6ef562f56424c91 /Classes/GameOverLayer.m
parent4457e0ce5099a81f8e6a366e353561b886725532 (diff)
downloadcartcollect-c55c499656ed089df63b049979487e9af9d109d6.tar.gz
cartcollect-c55c499656ed089df63b049979487e9af9d109d6.tar.bz2
cartcollect-c55c499656ed089df63b049979487e9af9d109d6.zip
Fixed game over transition
Fixes #189
Diffstat (limited to 'Classes/GameOverLayer.m')
-rwxr-xr-xClasses/GameOverLayer.m199
1 files changed, 0 insertions, 199 deletions
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 @@
1//
2// GameOverLayer.m
3// Cart Collect
4//
5// Created by iD Student Account on 7/19/11.
6// Copyright 2011 __MyCompanyName__. All rights reserved.
7//
8
9#import "GameOverLayer.h"
10#import "Cart_CollectAppDelegate.h"
11#import <sqlite3.h>
12#import "cocoslive.h"
13#import "MainMenuLayer.h"
14
15@implementation GameOverLayer
16
17+ (CCScene*)sceneWithScore:(int)score
18{
19 CCScene* scene = [CCScene node];
20
21 CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
22 [scene addChild:backgroundLayer];
23
24 GameOverLayer* layer = [[[GameOverLayer alloc] initWithScore:score] autorelease];
25 [scene addChild:layer];
26
27 return scene;
28}
29
30- (id)initWithScore:(int)score2
31{
32 self = [super init];
33
34 if (nil != self)
35 {
36 CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"];
37 backgroundImage.position = ccp(240, 160);
38 [self addChild:backgroundImage z:0];
39
40 score = score2;
41
42 scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200-320, 0, 0)];
43 [scoreField setFont:[UIFont systemFontOfSize:20.0f]];
44 [scoreField setBackgroundColor:[UIColor clearColor]];
45 [scoreField setText:[NSString stringWithFormat:@"%d", score2]];
46 CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip];
47 [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)];
48 [[[CCDirector sharedDirector] openGLView] addSubview:scoreField];
49
50 textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247-320, 216, 31)];
51 [textField setFont:[UIFont systemFontOfSize:20.0f]];
52 [textField setBackgroundColor:[UIColor clearColor]];
53 [textField setDelegate:self];
54 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
55 [textField setText:[defaults objectForKey:@"username"]];
56 [[[CCDirector sharedDirector] openGLView] addSubview:textField];
57
58 submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161-320, 0, 0)];
59 [submitSwitch setOn:[defaults boolForKey:@"submitScore"]];
60 [[[CCDirector sharedDirector] openGLView] addSubview:submitSwitch];
61
62 activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)];
63 [[[CCDirector sharedDirector] openGLView] addSubview:activityIndicator];
64
65 backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
66 [backButton setFrame:CGRectMake(240-154/2, 320-60-15-320, 154, 31)];
67 [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal];
68 [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside];
69 [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
70 [backButton.titleLabel setTextColor:[UIColor blackColor]];
71
72 [[[CCDirector sharedDirector] openGLView] addSubview:backButton];
73 }
74
75 return self;
76}
77
78- (void)onEnter
79{
80 [UIView beginAnimations:nil context:NULL];
81 [UIView setAnimationDuration:1.5f];
82 [UIView setAnimationCurve:UIViewAnimationCurveLinear];
83 scoreField.transform = CGAffineTransformMakeTranslation(0, 320);
84 textField.transform = CGAffineTransformMakeTranslation(0, 320);
85 submitSwitch.transform = CGAffineTransformMakeTranslation(0, 320);
86 backButton.transform = CGAffineTransformMakeTranslation(0, 320);
87 [UIView commitAnimations];
88}
89
90- (void)newgame
91{
92 backButton.enabled = NO;
93 textField.enabled = NO;
94 submitSwitch.enabled = NO;
95
96 const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String];
97 sqlite3_stmt* compiled_statement;
98
99 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
100 {
101 sqlite3_step(compiled_statement);
102 NSLog(@"awesome, %@", [textField text]);
103 } else {
104 NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database]));
105 }
106
107 if (submitSwitch.on)
108 {
109 [self submitScore];
110 } else {
111 [self exit];
112 }
113}
114
115- (void)submitScore
116{
117 [activityIndicator startAnimating];
118
119 CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self];
120 NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
121 [dict setObject:@"Classic" forKey:@"cc_category"];
122 [dict setObject:[textField text] forKey:@"cc_playername"];
123 [dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"];
124 [server sendScore:dict];
125 [server release];
126 [dict release];
127}
128
129- (BOOL)textFieldShouldReturn:(UITextField *)m_textField
130{
131 [m_textField resignFirstResponder];
132
133 return YES;
134}
135
136- (void)scorePostOk:(id)sender
137{
138 [activityIndicator stopAnimating];
139
140 // Score post successful
141 [self exit];
142}
143
144- (void)scorePostFail:(id)sender
145{
146 [activityIndicator stopAnimating];
147
148 // score post failed
149 tPostStatus status = [sender postStatus];
150 if( status == kPostStatusPostFailed ) {
151 NSLog(@"SERVER ERROR");
152 // an error with the server ?
153 // try again
154 }else if( status == kPostStatusConnectionFailed ) {
155 NSLog(@"CONNECTION FAILURE");
156 // a error establishing the connection ?
157 // turn-on wifi, and then try again
158 }
159
160 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];
161 [alertView show];
162 [alertView release];
163}
164
165- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
166{
167 if (buttonIndex == 0)
168 {
169 [self submitScore];
170 } else if (buttonIndex == 1)
171 {
172 [self exit];
173 }
174}
175
176- (void)exit
177{
178 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
179 [defaults setObject:[textField text] forKey:@"username"];
180 [defaults setBool:submitSwitch.on forKey:@"submitScore"];
181
182 [submitSwitch removeFromSuperview];
183 [submitSwitch release];
184
185 [textField removeFromSuperview];
186 [textField release];
187
188 [scoreField removeFromSuperview];
189 [scoreField release];
190
191 [activityIndicator removeFromSuperview];
192 [activityIndicator release];
193
194 [backButton removeFromSuperview];
195
196 [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
197}
198
199@end