Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By : Nathan Burba
Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By: Nathan Burba

Overview of this book

Cocos2d for iPhone is a robust but simple-to-use 2D game framework for iPhone. It is easy to use, fast, flexible, free, and Appstore approved. More than 2500 AppStore games already use it, including many best-seller games. Do you want to take your cocos2d game development skills to the next level and become more professional in cocos2d game design? Cocos2d for iPhone 1 Game Development Cookbook will help you reach that next level. You will find over 100 recipes here that explain everything from the drawing of a single sprite to AI pathfinding and advanced networking. Full working examples are emphasized. Starting with the first chapter, Graphics, you will be taken through every major topic of game development. You will find both simple and complex recipes in the book. Each recipe is either a solution to a common problem (playing video files, accelerometer steering) or a cool advanced technique (3D rendering, textured polygons). This cookbook will have you creating professional quality iOS games quickly with its breadth of working example code.
Table of Contents (15 chapters)
Cocos2d for iPhone 1 Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Playing video files


The cutscene is a concept that has existed since the early days of video games. Cutscenes are usually interspersed in between gameplay segments or shown when a game is loading. For more complex cutscenes it is often advantageous to use full motion video. In this recipe we will see how to insert a video into our game.

Getting ready

Please refer to the project RecipeCollection01 for full working code of this recipe.

How to do it...

This recipe requires the extra step of linking the MediaPlayer iOS framework to our project:

  1. Right-click your project under Groups & Files.

  2. Click Add | Existing Frameworks

  3. Under iOS SDK select MediaPlayer.framework

Keep in mind that RecipeCollection01 already has this library linked.

Now, execute the following code:

#import <MediaPlayer/MediaPlayer.h>

@interface Ch1_PlayingVideoFiles {
  MPMoviePlayerController *moviePlayer;
}

@implementation Ch1_PlayingVideoFiles

-(CCLayer*) runRecipe {
  //Load our video file
  NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"example_vid" ofType:@"mov"]];
  
  //Create a MPMoviePlayerController object
  moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

  //Register to receive a notification when the movie has finished playing.
  [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(moviePlayBackDidFinish:)
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:moviePlayer];

  //Set the movie's control style and whether or not it should automatically play.
  if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    //Use the new 3.2 style API.
    moviePlayer.controlStyle = MPMovieControlStyleNone;
    moviePlayer.shouldAutoplay = YES;
        
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    moviePlayer.view.frame = CGRectMake(45, 50, winSize.width-90, winSize.height-100);
    [[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view];
  } else {
    //Use the old 2.0 style API.
    moviePlayer.movieControlMode = MPMovieControlModeHidden;
    [self playMovie];
  }
  
  return self;
}

-(void)moviePlayBackDidFinish:(NSNotification*)notification {
  //If playback is finished we stop the movie.
  [self stopMovie];
}

-(void)playMovie {
  //We do not play the movie if it is already playing.
  MPMoviePlaybackState state = moviePlayer.playbackState;
  if(state == MPMoviePlaybackStatePlaying) {
    NSLog(@"Movie is already playing.");
    return;
  }
  
  [moviePlayer play];
}

-(void)stopMovie {
  //We do not stop the movie if it is already stopped.
  MPMoviePlaybackState state = moviePlayer.playbackState;
  if(state == MPMoviePlaybackStateStopped) {
    NSLog(@"Movie is already stopped.");
    return;
  }
  
  //Since playback has finished we remove the observer.
  [[NSNotificationCenter defaultCenter] removeObserver:self
    name:MPMoviePlayerPlaybackDidFinishNotification
        object:moviePlayer];

  //If the moviePlayer.view was added to the openGL view, it needs to be removed.
  if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    [moviePlayer.view removeFromSuperview];
  }
}

-(void)cleanRecipe {
  [super cleanRecipe];
  [self stopMovie];
  [moviePlayer release];
}

@end

How it works...

This recipe shows us how to load, play, and stop a movie.

  • Using MPMoviePlayerController:

    This recipe is merely the tip of the iceberg regarding movie playback. Movies can also be played back in fullscreen mode, in portrait mode, and using a variety of other options. Please refer to official Apple documentation when customizing and/or adding to this technique.

  • Using Objective-C observers :

    The observer pattern is not used very often while doing Cocos2d programming, but it is a powerful mechanism and it is the recommended way of knowing when your video has finished playback. You can read more about observers by referring to the official Objective-C documentation.

  • Movie file format :

    According to Apple documentation it is recommended that you compress your movies using H.264/MPEG-4 for video, AAC audio and one of the following file formats: MOV, MP4, MPV, 3GP.

    It is also recommended that your movies be no larger than 640x480 and run no faster than 30 FPS.

    The movie used in the recipe was created and encoded using Apple's iMovie software.

    For more information please consult official Apple iOS SDK documentation.