Game Development Reference
In-Depth Information
Listing 8-29. SwipeGestureController.m (swipeGesture:)
-(void)swipeGesture:(UISwipeGestureRecognizer*)swipeRecognizer{
CGSize gameSize=[self gameAreaSize];
UISwipeGestureRecognizerDirection direction=[swipeRecognizer direction];
CGPoint locationInView=[swipeRecognizer locationInView:actorsView];
CGPoint center=CGPointMake(0, 0);
float directionInRadians = DIRECTION_DOWN;
if (direction == UISwipeGestureRecognizerDirectionRight){
center.x=−20;
center.y=locationInView.y;
directionInRadians = DIRECTION_RIGHT;
} else if (direction == UISwipeGestureRecognizerDirectionDown){
center.x=locationInView.x;
center.y=−20;
directionInRadians = DIRECTION_DOWN;
} else if (direction == UISwipeGestureRecognizerDirectionLeft){
center.x=gameSize.width+20;
center.y=locationInView.y;
directionInRadians = DIRECTION_LEFT;
} else if (direction == UISwipeGestureRecognizerDirectionUp){
center.x=locationInView.x;
center.y=gameSize.height+20;
directionInRadians = DIRECTION_UP;
}
Comet* comet=[Comet comet:self withDirection:directionInRadians andCenter:center];
[self addActor:comet];
}
In Listing 8-29, we see the code that is called when a swipe gesture is recognized. To create the
effect we want, we are simply going to create a new Comet actor with the correct location and
direction. To figure out where the Comet should be placed and in what direction it travels, we simply
look at the direction property of swipeRecognizer .
We are not going to go into the details of how the Comet class is implemented; we have discussed
that at length already. If you are really interested, please review the source code provided.
We have looked at the built-in gesture recognizers available in iOS and have explored the basics
of how they work. In the next section, we will explore how movements of the device can be
incorporated into an application.
Interpreting Device Movements
Starting with the iPhone 4, iOS devices have an accelerometer and a gyroscopic sensor built into
them. There are several ways to use these sensors in an application. The two most common ways
are to simply respond to a motion event, which corresponds to shaking the device. The other
 
Search WWH ::




Custom Search