October 2009
Beginner
260 pages
7h 23m
English
Because our GameState class inherits from UIView, we can override its event handling
functions for our purposes. To show this behavior, we will add to our
example by creating a second GameState
named gsTest. We will modify
gsMain to change to the new state when
the user double-taps the screen, and switch back again when she
double-taps from gsTest:
//gsTest.h
@interface gsTest : GameState {
//no members necessary
}
@end
//gsTest.m
@class gsMain //forward declaration
@implementation gsTest
-(gsTest*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager
{
if (self = [super initWithFrame:frame andManager:pManager]) {
NSLog(@"gsTest init");
}
return self;
}
-(void) Render
{
CGContextRef g = UIGraphicsGetCurrentContext();
//fill background with blue
CGContextSetFillColorWithColor(g, [UIColor blueColor].CGColor);
CGContextFillRect(g,
CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
//draw text in black
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
[@"it works!" drawAtPoint:CGPointMake(10.0,20.0)
withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
if( numTaps > 1 ) {
[m_pManager doStateChange:[gsMain class]];
}
}
@endFor the return to gsTest, we need
to add the same touchesEnded function
to our gsMain class, but making
doStateChange call [gsTest class]. When compiled and run, ...
Read now
Unlock full access