Online Book Reader

Home Category

iPhone Game Development - Chris Craft [74]

By Root 1603 0
you make in your main files. Here's the correct header file for the changes we have made so far:

#import

@interface MainViewController : UIViewController {

}

- (void)updateCar:(CGFloat)positionInDegrees;

@end

Updating the player's score

You want to reward the player's success by adding points to his score. If the player does well, he gets a bonus; if the player makes a mistake, consider imposing a penalty such as taking away points.

Tip

Some people may hate statistics, but players love knowing theirs. Consider tracking your player's accomplishments during a game; for example, by total number of zombies slain or total amount of treasure found. Many players will replay a game to fully “complete” a level or game.

Let's start by creating a very simple scoring system and then allowing you to customize it as you see fit from there. Open the MainView.xib file in Interface Builder. Start in the Groups & Files listing in Xcode. Find the AmuckRacer folder and then locate the Resources folder; inside it you should find the MainView.xib file. Double-click it to have Interface Builder open it. Open the Library that is found inside the Interface Builder's Tools menu. Make sure you have the Options tab selected instead of the Media tab. Expand the Library folder, then the Cocoa Touch Plugin folder, and finally choose the Inputs & Values folder. Drag a Label control onto the Main View window. Set the label's text to Score: 0. Move the label to the location of your liking (consider placing it at the top in the middle of the screen).

We need to create mappings from Interface Builder's user interface to Xcode classes so that the two can talk with each other. From the Interface Builder's Tools menu, choose Inspector. Make sure that the Inspector window is focused on the Main View by click the Main View's window title. Once you have the Main View selected, you will see Main View appear in the title bar of the Inspector window.

Go to the fourth tab in the Inspector window, named Main View Identity. You should see the two class outlets you created earlier named car and road. Add another one called score by clicking the button that has a plus sign on it. Be sure to set its Type to UILabel. Your screen should now look like the one shown in Figure 5.16.

As before, you will now use Interface Builder's Write Class Files feature to update your Main View's class files with the new control. Make sure you have the Main View window selected before running the Write Class Files command. You should see a screen like Figure 5.17.

FIGURE 5.16

Adding a score counter to AmuckRacer


FIGURE 5.17

The Save dialog box of the Write Class Files feature


You can find this menu item under Interface Builder's File menu. Next, you will see a merge or replace prompt, as shown in Figure 5.18. Be certain you choose the Merge option; if you choose the Replace option, Interface Builder will replace your edited files with empty new ones. This is a painful lesson to learn!

You have now created the user interface for the player's score. You need to add the code that will update the UI. To do this, you will need a new variable named score that will keep up with the player's score for the duration of the game. By default we will add to the player's score upon updating of the game loop. Here are the code changes needed to update the player's score.

First, you will need to initialize the score variable:

int score = 0;

You will also need to modify the update method as follows:

- (void)update {

[self updateRoad];

score += 1;

}

FIGURE 5.18

Merge or Replace prompt


And finally, you should update the draw method to set the label's text to the player's score:

- (void)draw {

score.text = [NSString stringWithFormat: @”Score: d%”, currentScore];

}

Now when you run AmuckRacer, you should see the player's score at the top of the screen, as shown in Figure 5.19.

Staying alive

It's always important in an action game to decide how the player wins and loses. In this game you

Return Main Page Previous Page Next Page

®Online Book Reader