Online Book Reader

Home Category

iPhone Game Development - Chris Craft [128]

By Root 1601 0
can proceed to the next step. An MPMoviePlayerController is instantiated with the URL we just discussed:

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

Next, bind the event moviePlayBackDidFinish to the movie player event MPMoviePlayerPlaybackDidFinishNotification. This ensures that when the movie finishes playing you will receive notification and can take appropriate action:

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlayBackDidFinish:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayer];

Finally, set some movie player options and send the message play. The movie takes over the screen and begins to play:

moviePlayer.scalingMode = MPMovieScalingModeAspectFill;

moviePlayer.movieControlMode = MPMovieControlModeHidden;

moviePlayer.backgroundColor = [UIColor blackColor];

[moviePlayer play];

As a final note, we will look at the moviePlayBackDidFinish message. This is the message we bound to the movie player event MPMoviePlayerPlaybackDidFinishNotification. In this novelty example we simply play the movie again, but in your games you will probably want to clean up any movie-related objects and resume your game:

-(void)moviePlayBackDidFinish: (NSNotification*)notification{

moviePlayer = [notification object];

[moviePlayer play];

}

Cross-Reference

For more on using the movie player, see Chapter 4.

Discovering Geolocation

Geolocation data can be collected through Core Location services if the device has support for it. You saw this earlier in the compass section, but this time we are going to look at the location data itself and not the heading.

Altimeter is an example that uses Core Location to discover the altitude of your current location. Download Altimeter from http://appsamuckcom/day19.html and fire it up to discover the altitude where you are (Figure 9.13).

FIGURE 9.13

The Altimeter example illustrates how to use Core Location to determine altitude.


All in all, Altimeter has hardly any code. It is simply regurgitating the information returned from Core Location. The code that is executed on application startup should look very familiar to the code you saw in the earlier compass example:

locmanager = [[CLLocationManager alloc] init];

[locmanager setDelegate:self];

[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];

[locmanager startUpdatingLocation];

This time we implemented locationManager:didUpdateToLocation:, which is called when location data is reported to your application. The parameter newLocation contains the altitude information we are looking for in this app, but don't worry, you can get the latitude and longitude from the same place:

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

fromLocation:(CLLocation *)oldLocation {

altitude.text =

[NSString stringWithFormat: @”%.2f m”, newLocation.altitude];

}

In the event that location information could not be retrieved, we respond to locationManager:didFailWithError: to clear the display:

- (void)locationManager:(CLLocationManager *)manager

didFailWithError:(NSError *)error {

altitude.text = @”0.00 m”;

}

Note

Altitude data is only available on devices that have a GPS chip, and is only available where there is a GPS fix (for example, outdoors with a clear view). Wi-Fi-based location services, such as those found on the original iPhone and the iPod touch, cannot obtain altitude information.

Stepping into the Third Dimension

As you probably know, the iPhone supports OpenGL ES for 3-D graphics. Just that fact alone speaks volumes about the capability of the device. OpenGL is a huge topic that could fill many books in an effort to cover all the details and nuances introduced with this powerful API. While you are not going to learn all the details of OpenGL in this section, we have provided you with this primer to help introduce you to programming in 3-D on the iPhone.

Analyzing the OpenGL ES template

To get started on this journey,

Return Main Page Previous Page Next Page

®Online Book Reader