Game Development Reference
In-Depth Information
Listing 4-20. CoinsController.m (tapGesture:, partial)
- (void)tapGesture:(UIGestureRecognizer *)gestureRecognizer{
if ([coinsGame remaingTurns]>0 && acceptingInput){
UITapGestureRecognizer* tapRegognizer = (UITapGestureRecognizer*)gestureRecognizer;
CGPoint location = [tapRegognizer locationInView:coinsView];
Coord coinCoord = [self coordFromLocation:location];
if (!isFirstCoinSelected){//first of the pair
isFirstCoinSelected = true;
firstSelectedCoin = coinCoord;
[self stopCoinAt: firstSelectedCoin];
} else {
///shown in Listing 4-24
}
}
UIGestureRecognizer that triggered this event as an argument. We will
acceptingInput
false during animations in order to prevent the user from selecting coins midtransition,
gestureRecognizer to
UITapGestureRecognizer called tapRecognizer , we get the location of the tap by calling
and passing in coinsView . The location of the tap is returned as a CGPoint called
location . In order to know which coin was tapped, we convert location to Coord by calling
coordFromLocation , shown in Listing 4-21.
Listing 4-21. CoinsController.m (coordFromLocation:)
-(Coord)coordFromLocation:(CGPoint) location{
CGRect coinsFrame = [coinsView frame];
Coord result;
result.col = location.x / coinsFrame.size.width * [coinsGame colCount];
result.row = location.y / coinsFrame.size.height * [coinsGame rowCount];
return result;
}
The task coordFromLocation: takes a CGpoint and converts it into a Coord that will tell us which coin
the user tapped. To find the coin, the x value of the location is divided by the width of the coinsView
and then multiplied by the number of columns. The row is calculated in a similar way.
In Listing 4-20, after we determine which coin was tapped and store the result as the variable
coinCoord , we check whether the coin was previously selected. If not, we set isFirstCoinSelected
to true , record which coin was selected, and call stopCoinAt: , shown in Listing 4-22.
 
Search WWH ::




Custom Search