Game Development Reference
In-Depth Information
the skin of the screen. Using algorithms similar to image processing and edge detection,
this system can compute the extent of a touch event.
Example Program
Included in the source code accompanying this topic is an example of the particle ex‐
plosion program from Chapter 8 that uses touch screen input instead of a mouse click.
The code for a Cocoa touch Objective-C event is as follows:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch* touch = [[event touchesForView:self] anyObject];
firstTouch = [touch locationInView:self];
self.status = YES;
[self trigger];
where firstTouch is defined by CGPoint firstTouch; in the header file. The CGPoint
is a Cocoa touch object capable of storing an ( x , y ) coordinate in the display view's co‐
ordinate system. We can then use firstTouch.x and firstTouch.y later in our program
to give a location to the particle explosion.
As you can see, it is very similar to a mouse-based event. One big difference is that you
could adapt the code to handle multitouch events. Computers recognize only one mouse
cursor at a time, but with a touch screen you can register multiple clicks simultaneously.
In iOS you must first enable delivery of multiple touch events by setting the multiple
TouchEnabled property of your view to YES ; the default is NO . Next you must create a
class to keep track of multiple TParticleExplosion structures. Then it is as simple as
polling the position of the start points of multiple touches to trigger multiple explosions.
The Objective C code to store the start points of multiple touch events would look like:
- (void)storeTouchPoints:(NSSet *)touches{
if ([touches count] > 0) {
for (UITouch *touch in touches) {
CGPoint *point = (CGPoint
*)CFDictionaryGetValue(touchBeginPoints, touch);
if (point == NULL) {
point = (CGPoint *)malloc(sizeof(CGPoint));
CFDictionarySetValue(touchBeginPoints, touch, point);
*point = [touch locationInView:view.superview];
Search Nedrilad ::

Custom Search