Game Development Reference
In-Depth Information
[sparks removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Moved: %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];
}
}
@end
In Listing 8-3, we see the four tasks discussed earlier. The class UIResponder defines each task,
which is the super-class of UIView . These methods are called as the user interacts with the screen.
By implementing them, we can add our custom behavior. The task touchesBegan:withEvent : is
called when a finger touches the screen. When this happens, we create a new NSMutableArray to
keep track of all of the sparks we are going to add. Any of the other three tasks could be called
next. If the user moves his or her finger, or places another new finger on the screen, the task
touchesMoved:withEvent : is called. In this task, we create and a new spark for each touch. We use
the number of touches minus 1, to specify the variant or color of the spark, which is then added the
scene. The spark is just a simple particle-like class that removes itself after five seconds.
There are two ways that a touch event can end. The first way is when touchesEnded:withEvent :
is called after the user has removed all of his fingers. In this case, we do the same thing we did
in touchesMoved:withEvent: : we create a spark for each touch. We also clear the NSMutableArray
sparks, to remove reference to the spark objects.
The other way a touch event can end is by it being cancelled. An event is cancelled when something
else in the system happens that makes completing the event inappropriate. When this tasks is
called, we removed all of the Spark objects we have created since touchesBegan:withEvent : was last
called, undoing the work we have done up until this point.
The touchesCancelled:withEvent : task can be called when switching applications or having a
dialog popup. Another perhaps more common way for touch events to be cancelled is if a gesture
recognizer is triggered. A gesture recognizer, as defined by the class UIGestureRecognizer , is
an object that inspects the touch events on a UIView and responds when the touches can be
considered a specific gesture, like a tap, pinch, or swipe.
To experiment with the touchesCancelled:withEvent : tasks, do a two-finger double tap. Notice that
after the first tap, there are two sparks where you fingers touched. After the second tap, the Spark
objects that where added get removed. This is because touchesCancelled:withEvent : was called
when the two-figure double-tap gesture recognizer was activated by the touch events.
The fact that gesture recognizers are able to cancel touch events allows developers to mix and match
any number of gestures on a UIView without worrying about the gestures conflicting. In addition to
canceling the touch events, gestures can also cancel each other. For example, a pinch gesture and
a two-finger drag gesture both start the same way, with two fingers landing on the screen at about
the same time. These two gestures could easily conflict if there was no mechanism in place to cancel
each other. Let's move on a see how these touch events apply to the game-related classes.
 
Search WWH ::




Custom Search