GamePlayViewController

type:
extension
author:
Matt King
description:
A view controller for SPView.
compatible:
v1.1
tag:
SPView, SPStage

Usage

The GamePlayViewConroller is a view controller for SPView. It is a subclass of UIViewController so it can be used on a navigation stack or as a model view controller.

GamePlayViewController responds to the application moving to/from the background, and listens for a “StopGame” NSNotification.

It also has SPStage property. Set this property with your stage before presenting the view controller.

Changelog

  • 2011/05/06 23:22: First public version

Source Code

GamePlayViewController.h

#import <UIKit/UIKit.h>
#import "Sparrow.h"
 
@interface GamePlayViewController : UIViewController {
	SPView *_sparrowView;
}
 
@property (nonatomic, retain) SPStage *stage;
 
- (void)stopGameNotification;
 
- (void)onTransitionToActive:(NSNotification *)sender;
- (void)onTransitionToInactive:(NSNotification *)sender;
 
 
@end

GamePlayViewController.m

#import "GamePlayViewController.h"
 
@implementation GamePlayViewController
 
@synthesize stage;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
		_sparrowView = [[SPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 320.0)];
 
    }
    return self;
}
 
- (void)loadView {
	[self setView:_sparrowView];
	[_sparrowView release];
}
 
#pragma mark -
#pragma mark View Life Cycle
 
- (void)viewDidLoad {
    [super viewDidLoad];
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTransitionToInactive:) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopGameNotification) name:@"StopGame" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTransitionToActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
 
}
 
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	self.stage = nil;
}
 
- (void)viewDidUnload {
    [super viewDidUnload];
}
 
#pragma mark -
#pragma mark Rotation
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
 
#pragma mark -
#pragma mark Accessor Methods
 
- (void)setStage:(SPStage *)aStage {
	if (aStage) {
		SPView *spView = (SPView *)[self view];
 
		spView.stage = aStage;
		spView.frameRate = 30.0;
		[spView start];
 
		[SPAudioEngine start:SPAudioSessionCategory_AmbientSound];
	}
}
 
#pragma mark -
#pragma mark Notifications
 
- (void)stopGameNotification {
    SPView *spView = (SPView *)[self view];
	[spView stop];
 
	[SPAudioEngine stop];
 
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}
 
- (void)onTransitionToActive:(NSNotification *)sender {
    SPView *spView = (SPView *)[self view];
    [spView start];
 
    [SPAudioEngine start];
}
 
- (void)onTransitionToInactive:(NSNotification *)sender {
    SPView *spView = (SPView *)[self view];
    [spView stop];
 
    [SPAudioEngine stop];
}
 
 
#pragma mark -
#pragma mark Memory Managment
 
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
    // Release any cached data, images, etc. that aren't in use.
}
 
- (void)dealloc {
    [self.stage release];
 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"StopGame" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
 
    [super dealloc];
}
 
@end

Discussion

No comments so far. Feel free to edit this part of the page.

  extensions/gameplayviewcontroller.txt · Last modified: 2015/09/14 11:15 by 127.0.0.1
 
Powered by DokuWiki