Online Book Reader

Home Category

iPhone Game Development - Chris Craft [33]

By Root 1517 0
= [NSString stringWithFormat:

@”High Score: %@”, highScore];

self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

}

- (void)onEmailButtonClick {

NSString* highScore = [Helper getUserValueForKey:

@”highscore” withDefault:@”0”];

NSString *emailBody = [NSString stringWithFormat:

@”Do you think you can beat my high score at AmuckSlider?

My current high score is:%@”, highScore];

[Helper sendEmailWithSubject:@”Beat this score!” withBody:emailBody];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (void)dealloc {

[highScoreLabel release];

[super dealloc];

}

@end

You probably noticed that sendEmailWithSubject and getUserValueForKey are method calls from the static class Helper. As you know, it's best to encapsulate code that you will be using time and time again. The full listings for the methods sendEmailWithSubject and getUserValueForKey are right here:

+ (void)sendEmailWithSubject:(NSString*)aSubject withBody:(NSString*)aBody{

NSString *urlString = [NSString stringWithFormat:

@”mailto:?subject=%@&body=%@”, aSubject, aBody];

urlString = [urlString stringByReplacingOccurrencesOfString:

@” “ withString:@”%20”];

NSURL* mailURL = [NSURL URLWithString: urlString];

[[UIApplication sharedApplication] openURL: mailURL];

}

+ (NSString*)getUserValueForKey:(NSString*)aKey

withDefault:(NSString*)aDefaultValue {

NSString *result = [[NSUserDefaults standardUserDefaults]

stringForKey: aKey];

if (result == nil || [result isEqualToString:@””]) {

return aDefaultValue;

}

return result;

}

Managing level data

We want our game to have several different levels to keep players' interest, and we need to store data for each of our levels. In addition, we want our game to have different degrees of challenge, such as easy, medium, and hard. Since this is a puzzle game and you have to be clever to figure out the patterns, we have decided to make the degree of challenge a literal degree. The levels of difficulty are Bachelors for easy, Masters for medium, and Doctorate for hard. We have eight levels for each degree, for a total of 24 levels.

We need to set up some interfaces to define this model. The ultimate goal is to create an easy way for us to get level data and to separate that concern from the rest of the system. In order to host the class model, we've created a new pair of files, Game.h and Game.m. In these files we have defined a few interfaces and enumerations to fulfill our requirements.

The whole purpose of the Game class is to implement the following methods:

+ (Difficulty)getBestDifficulty;

+ (int)getBestLevelForDifficulty:(Difficulty)aDifficulty;

+ (int)getCurrentLevelIndex;

+ (void)setCurrentLevelIndex;

+ (Level*)getLevelAtLevelIndex:(int)aLevelIndex;

These methods will help us easily access information necessary to the game system. Here is a short description of what these methods do:

getBestDifficulty: Initially, a player can only play at the Bachelors level; he must earn the right to play at the Masters and Doctorate levels. This method indicates at which level a player is allowed to play.

getBestLevelForDifficulty: A player must start at level one and complete it before he is allowed to progress to level two, and so on. This method indicates the level a player is allowed to play next for the difficulty specified.

getCurrentLevelIndex: This method returns the player to the level he last played on startup without needing to go to the menu. This is convenient for returning to the game after being interrupted by a phone call, for example.

setCurrentLevelIndex: This method allows you to set the current level index. Call this method to record when a player beats a level so the next level will be unlocked.

getLevelAtLevelIndex: This method returns data necessary to describe a given level. A level is defined by its grid size, tile size, and tile data. The tile data defines the location in grid coordinates where the tile currently resides. It defines the index of where the

Return Main Page Previous Page Next Page

®Online Book Reader