Online Book Reader

Home Category

iPhone Game Development - Chris Craft [52]

By Root 1592 0
have added all the required files and frameworks, your project should look like Figure 4.12.

FIGURE 4.12

iPhone SDK frameworks added to the iDrum application


Expand the Main View folder under Groups & Files below the iDrum project. Open the MainView.h file in Xcode. You will need to add an import line to include the SoundEngine.h file:

#import “SoundEngine.h”

You will also add a define directive that will declare a constant for setting the listener distance:

// Used for creating a realistic sound field

#define kListenerDistance 1.0

Next you will need to add an IBAction to capture touches on the drum image and a startBackgroundMusic:

- (IBAction)drum;

- (void) startBackgroundMusic;

Once you are done making all of these changes to your MainView.h file, it should look something like this:

#import

#import “SoundEngine.h”

// Used for creating a realistic sound field

#define kListenerDistance 1.0

@interface MainView : UIView

{

}

- (IBAction)drum;

- (void) startBackgroundMusic;

@end

You should finish building the screen in Interface Builder before beginning work on the matching MainView.m file. In the Groups & Files panel, find the Resources folder and expand it. Inside you should be able to locate the MainView.xib file. Double-click this file to open it in Interface Builder. Open Interface Builder's Library by choosing Tools⇒Library. Find the Image View object located under Library⇒Cocoa Touch⇒Data Views. Drag the Image View over to the Main View window.

Take the Image View and resize it to fill the entire screen. Now open Interface Builder's Inspector tool by choosing Tools⇒Inspector. Make sure the Image View is selected in the Main View's window. Click on the left-most tab at the top of the Inspector window. It should be named Image View Attributes.

Using the pull-down menu, assign the drum.png file to the Image field, and set the Mode to Scale To Fill. This ensures that the image expands to fill all available space. Set Alpha to 1.00 so the image will be completely opaque (Figure 4.13).

FIGURE 4.13

Main View Attributes dialog box


Now select the Main View window itself. The Inspector window should be titled Main View Attributes. Again, set the Mode to Scale To Fill and Alpha to 1.00. Set the background color to Iron from the color list. Under the Drawing section, check the Opaque check box, and under the Interaction section, check the User Interaction Enabled check box.

At the top of the Inspector window, click on the fourth tab named Main View Identity. Under Class Actions, click the small plus button to add a new Action named drum. Now you will open the MainView.m file. First, you need to add a new sound enumeration to the top of this file:

enum

{

kSound_Drumbeat = 0,

kNumSounds

};

The purpose of this enumeration is to simply keep track of how many sounds there are in the application and which one to play at any given time. In this case, the iDrum application has only the drumbeat sound, which is located at index 0. Since the drumbeat sound is set to 1, and kNumSounds is set to 1, everything matches and you have the correct number of sounds available.

Next, you need to add an internal sound variable to track the current playing sound:

UInt32 _sounds[kNumSounds];

For this application it makes sense to have some background music playing in an infinite loop. This way the user has something playing to add to instead of being responsible for everything. You start the background music when the Main View window is loaded:

-(void)awakeFromNib

{

[self startBackgroundMusic];

}

Now when the application is started and it loads the Main View window, this in turn calls the startBackgroundMusic method on the view:

- (void) startBackgroundMusic

{

NSBundle* bundle = [NSBundle mainBundle];

// Note that each of the Sound Engine functions defined in

// SoundEngine.h returns an OSStatus value.

// Although the code in this application does not check for errors,

// you'll want to add error checking code

// in your

Return Main Page Previous Page Next Page

®Online Book Reader