Last active
August 29, 2015 14:10
-
-
Save sarah-j-smith/83aaf53bba5aa6f61d9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AnimatedTutorial.h | |
// SpaceBotAlpha | |
// | |
// Created by Sarah Smith on 26/09/2014. | |
// Copyright (c) 2014 Smithsoft Pty Ltd. All rights reserved. | |
// | |
// Show a movie - eg a .mp4 file - in a windowed frame of a Cocos2D scene. | |
// Has an "OK, got it" button which dismisses the video. If the video has | |
// never been seen before then the button enables when the video is nearly | |
// finished, otherwise it enables immediately. | |
// | |
// The video will keep playing again, after a short pause between plays | |
// until the "OK, got it" button has been pressed. | |
// | |
// The size of the video relative to the scene/screen is determined by an | |
// empty CCNode in the CCB file, which is assigned to _videoFrame when the | |
// CCB loads. The AnimatedTutorial class is the type specified for the root | |
// node in the CCB so loading the CCB will instantiate the class. | |
@interface AnimatedTutorial : CCScene | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AnimatedTutorial.m | |
// SpaceBotAlpha | |
// | |
// Created by Sarah Smith on 26/09/2014. | |
// Copyright (c) 2014 Smithsoft Pty Ltd. All rights reserved. | |
// | |
#import <MediaPlayer/MediaPlayer.h> | |
#define INTER_LOOP_PAUSE 1.0f | |
@implementation AnimatedTutorial | |
{ | |
CCLabelBMFont *_title; | |
CCSprite9Slice *_frame; | |
PlayButton *_gotItButton; | |
NSString *_videoName; | |
BOOL _moviePlaybackStopped; | |
MPMoviePlayerController *_movie; | |
CCNode *_videoFrame; | |
UIView *_videoFrameView; | |
} | |
- (void)moviePlayBackDidFinish:(NSNotification *)notification | |
{ | |
[_gotItButton setEnabled:YES]; | |
[_gotItButton pulseButton]; | |
if (_moviePlaybackStopped) return; | |
CCActionDelay *wait = [CCActionDelay actionWithDuration:INTER_LOOP_PAUSE]; | |
CCActionCallFunc *run = [CCActionCallFunc actionWithTarget:self selector:@selector(playVideo)]; | |
[self runAction:[CCActionSequence actions:wait, run, nil]]; | |
} | |
- (void)moviePlayBackStateDidChange:(NSNotification *)notification | |
{ | |
// | |
} | |
- (void)initializeMoviePlayer | |
{ | |
UIView *mainView = [[CCDirector sharedDirector] view]; | |
CGRect mainFrameUI = [mainView frame]; | |
CGSize mainViewSizeUI = mainFrameUI.size; | |
CGSize mainFrameSz = [[CCDirector sharedDirector] viewSize]; | |
CGSize videoFrameSz = [_videoFrame contentSizeInPoints]; | |
CGSize ratio = CGSizeMake(videoFrameSz.width/mainFrameSz.width, videoFrameSz.height/mainFrameSz.height); | |
CGSize videoFrameUISz = CGSizeMake(ratio.width * mainViewSizeUI.width, ratio.height * mainViewSizeUI.height); | |
// For UIKit the origin point is top-left | |
CGPoint videoFrameOrigin = [_videoFrame convertToWorldSpace:ccp(0.0f, videoFrameSz.height)]; | |
CGPoint videoFrameOriginUI = [[CCDirector sharedDirector] convertToUI:videoFrameOrigin]; | |
CGRect frameUI; | |
frameUI.origin = videoFrameOriginUI; | |
frameUI.size = videoFrameUISz; | |
_videoFrameView = [[UIView alloc] initWithFrame:frameUI]; | |
[_videoFrameView setBackgroundColor:[UIColor redColor]]; | |
[mainView addSubview:_videoFrameView]; | |
_movie = [[MPMoviePlayerController alloc] init]; | |
[_movie setRepeatMode:(MPMovieRepeatModeNone)]; | |
[_movie setControlStyle:MPMovieControlStyleNone]; | |
[_movie setScalingMode:MPMovieScalingModeAspectFit]; | |
[_movie setMovieSourceType:(MPMovieSourceTypeStreaming)]; | |
CGRect movieFrame; | |
movieFrame.size = frameUI.size; | |
[[_movie view] setFrame:movieFrame]; // player's frame must match parent's | |
[_videoFrameView addSubview: [_movie view]]; | |
[_videoFrameView bringSubviewToFront:[_movie view]]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(moviePlayBackDidFinish:) | |
name:MPMoviePlayerPlaybackDidFinishNotification | |
object:_movie]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(moviePlayBackStateDidChange:) | |
name:MPMoviePlayerPlaybackStateDidChangeNotification | |
object:_movie]; | |
} | |
- (void)tearDownMoviePlayer | |
{ | |
[_movie stop]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
[[_movie view] removeFromSuperview]; | |
[_videoFrameView removeFromSuperview]; | |
_movie = nil; | |
} | |
- (void)playVideo | |
{ | |
NSString *introVideoPath = [[NSBundle mainBundle] pathForResource:_videoName ofType:@"m4v"]; | |
NSAssert(introVideoPath, nil); | |
if (_movie == nil) | |
{ | |
[self initializeMoviePlayer]; | |
} | |
NSURL *movieUrl = [NSURL fileURLWithPath:introVideoPath]; | |
[_movie setContentURL:movieUrl]; | |
[_movie prepareToPlay]; | |
[_movie play]; | |
} | |
- (void)okPressed:(id)sender | |
{ | |
CCScene *gameScene = nil; | |
[self tearDownMoviePlayer]; | |
gameScene = [CCBReader loadAsScene:@"MainScreen/MainScene"]; | |
[[CCDirector sharedDirector] replaceScene:gameScene withTransition:[CCTransition transitionFadeWithDuration:1.0f]]; | |
} | |
- (void)onEnter | |
{ | |
[super onEnter]; | |
NSString *tutName = tutorialNameForIndex([[GameData sharedGameData]currentTutorial]); | |
BOOL animationHasBeenSeen = [[[GameData sharedGameData] hintsDisplayed] containsObject:tutName]; | |
[_gotItButton setEnabled:animationHasBeenSeen]; | |
[_gotItButton setTarget:self withSelector:@selector(okPressed:)]; | |
if (!animationHasBeenSeen) | |
{ | |
CCActionDelay *waitABit = [CCActionDelay actionWithDuration:7.0f]; | |
CCActionCallBlock *enableButton = [CCActionCallBlock actionWithBlock:^{ | |
[_gotItButton setEnabled:YES]; | |
[self setUpLevelPreview]; | |
}]; | |
[self runAction:[CCActionSequence actions:waitABit, enableButton, nil]]; | |
[[GameData sharedGameData] markHintDisplayed:tutName]; | |
} | |
} | |
- (void)onEnterTransitionDidFinish | |
{ | |
[super onEnterTransitionDidFinish]; | |
[self initializeMoviePlayer]; | |
if ([[GameData sharedGameData] currentLevel] == 0) | |
{ | |
if ([[GameData sharedGameData] currentTutorial] == TutorialBuilding) | |
{ | |
_videoName = @"01_build-iphone"; | |
NSString *titleString = NSLocalizedString(@"Back to the sides & score!", nil); | |
[_title setString:titleString]; | |
} | |
else if ([[GameData sharedGameData] currentTutorial] == TutorialPowerups) | |
{ | |
_videoName = @"02_collide-iphone"; | |
NSString *titleString = NSLocalizedString(@"Aim clear of corners!", nil); | |
[_title setString:titleString]; | |
} | |
} | |
else if ([[GameData sharedGameData] currentLevel] == 1) | |
{ | |
NSString *titleString = NSLocalizedString(@"Use your shields Alpha!", nil); | |
[_title setString:titleString]; | |
_videoName = @"03_squid-iphone"; | |
} | |
else | |
{ | |
NSLog(@"##### Error: No animated tutorial for this level!"); | |
} | |
[self playVideo]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment