iPhone Game Development - Chris Craft [119]
return cell;
default:
cell.textLabel.text = @”About”;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
}
The method tableView:viewForHeaderInSection: has been used to customize the appearance of the head on each of our sections. All we need to do is supply a UIView for each header callback:
- (UIView *)tableView: (UITableView *)tableView viewForHeaderInSection:
(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
self.tableView.bounds.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0,
headerView.bounds.size.width, 30)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textColor = [UIColor whiteColor];
if (section == 0)
headerLabel.text = @”Game”;
else
headerLabel.text = @”Preferences”;
[headerView addSubview:headerLabel];
return headerView;
}
There are a handful of methods that push a view on the UINavigationBar. The one here serves as a sample of how to accomplish this. The push occurs when pushViewController:animated: is called:
- (void) startNewInternetGame {
NewInternetGameMenuViewController *newInternetGameMenuViewController =
[[NewInternetGameMenuViewController alloc]
initWithNibName:@”NewInternetGameMenuViewController” bundle:nil];
[self.navigationController
pushViewController:newInternetGameMenuViewController animated:YES];
[newInternetGameMenuViewController release];
}
A little deeper in the code, the Game View is loaded. Following is an example of how this is done. This is where the abstraction we talked about earlier in the chapter comes into play. The GameViewController is created and a game connection is injected into it. This injection type is based on the game class name we pass to GameConnection:createWithName:delegate: as follows:
GameViewController *gameViewController = [[GameViewController alloc] initWithNibName:@”GameViewController” bundle:nil];
gameViewController.gameConnection = [GameConnection
createWithName:@”InternetGameConnection” delegate:gameViewController];
gameViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
self presentModalViewController:gameViewController animated:YES [gameViewController release];
Finally, the method tableView:didSelectRowAtIndexPath: is implemented to fire the appropriate response to the main menu selection:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
[self startNewHeadToHeadGame];
break;
case 1:
[self startNewNearbyGame];
break;
case 2:
[self startNewInternetGame];
break;
case 3:
[self continueGame];
break;
}
}
else {
switch (indexPath.row) {
case 0:
[self showSettings];
break;
case 1:
[self showAbout];
break;
}
}
}
Making calls to Web services
At this point you can handle many of the basics of game development. A new challenge is connecting to other players who are connected to the Internet. We discussed this feature earlier, and fortunately, it is actually a very straightforward process to code on the client.
Earlier you reviewed the GameConnection class and how it is used to abstract the connection details away from the game. We are now going to focus on the class InternetGameConnection that extends the class GameConnection and handles Internet-based connections:
#import #import “GameConnection.h” #define SERVICE_END_POINT @”http://services.appsamuck.com/turnbased/” @interface InternetGameConnection : GameConnection { NSString *serviceEndPoint; NSString *responseData; NSURLConnection *urlConnection; } @property (nonatomic, retain) NSString *responseData; - (void) apiConnectWithSessionId: (NSString*)sessionId; - (void) apiConnectWithPlayerName: