Online Book Reader

Home Category

iPhone Game Development - Chris Craft [36]

By Root 1525 0
File dialog box, choose iPhone OS⇒Cocoa Touch Classes⇒UIViewController subclass.

5. Name the class MenuViewController.m. Make sure the check box Also create MenuViewController.h is checked, then click Finish.

6. Choose the group Resources.

7. Again, choose File⇒New File to launch the New File dialog box.

8. This time, in the New File dialog box, choose iPhone OS⇒User Interfaces⇒View XIB. Name it MenuView.xib.

Tip

If you are creating a new class and an XIB to match, you will need to recompile the app before you can connect IBActions and UIOutlets to controls in Interface Builder.

Now let's add some controls to the view. Double-click the MenuView.xib file to open the menu user interface in Interface Builder. If you have downloaded the code and are following along, you should see a screen that looks like the one shown in Figure 3.10.

FIGURE 3.10

Menu View in Interface Builder


Notice in the previous figure that the third button is blurry. That's because the text from two different buttons is overlapping. This is not a problem, because we manage which button is shown at run-time in the class MenuViewController. When you click New Game from the Game View, you are presented with a menu that has the buttons Levels and Help on it (Figure 3.11).

FIGURE 3.11

First page of the menu screen


When you click Levels, you are presented with the three levels of difficulty: Bachelor, Masters, and Doctorate (Figure 3.12).

FIGURE 3.12

Choosing the level of difficulty from the menu screen


After choosing the difficulty, you will be presented with levels One through Eight (Figure 3.13). Finally, you can click the Help button to display a list of instructions.

Notice that this is not a very attractive set of screens. We will come back later and polish up the menu. You will find that if you spend time polishing as you go along you will get caught up in the details too early in the process. If you do this you can lose your focus on the big picture and have trouble getting reoriented at each stage of the process.

Another problem with polishing too early is that if you have to rethink or change anything, you will have to throw away work and time that you cannot recoup. Take it from us: Get the app working and functional, then come back and polish it up. You will save time and find the polishing process fun and rewarding.

FIGURE 3.13

Choosing the level from the menu screen


Now open the file MenuViewController.h and you will see the following:

@protocol MenuViewDelegate

@optional

-(void)loadLevel:(int)levelIndex;

@end

@interface MenuViewController : UIViewController {

Difficulty difficulty;

int level;

id delegate;

UIButton *levelNumberButton[8];

IBOutlet UIButton *levelButton;

IBOutlet UIButton *helpButton;

IBOutlet UIButton *bachelorsButton;

IBOutlet UIButton *doctorateButton;

IBOutlet UIButton *mastersButton;

}

@property (nonatomic, retain) id /**/ delegate;

- (IBAction)onLevelClick;

- (IBAction)onHelpClick;

- (IBAction)onBachelorsClick;

- (IBAction)onDoctorateClick;

- (IBAction)onMastersClick;

- (void)showRoot:(bool)aHidden;

- (void)showDifficulity:(bool)aHidden;

- (void)showLevels:(bool)aHidden;

@end

We have used two styles of building a user interface on this controller in order to give you a little variety. The buttons defined with IBOutlet UIButton *… are connected at design time using Interface Builder. When connecting with Interface Builder, you will need to connect the IBOutlet and the IBAction using the same method described in the Hello World example from Chapter 2. The other style used is to create the buttons at run-time when the MenuViewController class is instantiated. After these buttons are created, they are stored in this array:

UIButton *levelNumberButton[8];

In the example code file MainViewController.m you will find the method initLevelNumberButtons (Listing 3.2). This method creates the buttons that are stored in the array.

Listing 3.2

The Method initLevelNumberButtons

Return Main Page Previous Page Next Page

®Online Book Reader