Game Development Reference
In-Depth Information
Looking At the Event Code
The implementation of this simple demonstration shows how we receive the touch events and create
actors out of each touch on the screen. There are two classes involved:
TouchEventsController
that
extends
GameController
and
TouchEventsView
that extends
UIView
. Let's start by looking the class
TouchEventsView
. The header is shown in Listing 8-2.
Listing 8-2. TouchEventsView.h
@interface TouchEventsView : UIView{
IBOutlet TouchEventsController* controller;
NSMutableArray* sparks;
}
IBOutlet
so we can wire the
TouchEventsView
up to a
. We also see an
NSMutableArray
called
sparks
. This collection is used to
: is called. Let's take a look at the implementation of the class
, as shown on Listing 8-3.
TouchEventsView.m
@implementation TouchEventsView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Begin: %i", [touches count]);
sparks=[NSMutableArray new];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Cancelled: %i", [touches count]);
for (Spark* spark in sparks){
[controller removeActor:spark];
}
[sparks removeAllObjects];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Ended: %i", [touches count]);
int count=[touches count];
for (UITouch* touch in touches){
Spark* spark=[Spark spark:count-1 At:[touch locationInView:self]];
[controller addActor:spark];
[sparks addObject:spark];
}