Game Development Reference
In-Depth Information
Listing 4-22. CoinsController.m (stopCoinAt:)
-(void)stopCoinAt:(Coord)coord{
UIImageView* coinView = [[coinsView subviews] objectAtIndex:[coinsGame indexForCoord:coord]];
NSNumber* coin = [coinsGame coinForCoord:coord];
UIImage* image = imageForCoin([coin intValue]);
[coinView stopAnimating];
[coinView setImage: image];
}
The task
stopCoinAt:
takes the coordinate of the coin to stop spinning. This is done by first finding
the appropriate
UIImageView
representing the coin to be stopped, called
coinView
. We stop the
spinning animation by calling
stopAnimation:
, but we don't want to just stop it, we want to display
the face of the coin. We can find the correct image to use by first calling
coinForCoord
in
coinsGame
to figure out the type of coin it is and then calling
imageForCoin
to return the
UIImage
we want to
use. We simply call
setImage
on
coinView
and pass in the
UIImage
named
image
. The function
imageForCoin
simply returns a
UIImage
based on the
int
that is passed in, as shown in Listing 4-23.
Listing 4-23. CoinsController.m (imageForCoin(int))
UIImage* imageForCoin(int coin){
if (coin == COIN_TRIANGLE){
return [UIImage imageNamed:@"coin_triangle0001"];
} else if (coin == COIN_SQUARE){
return [UIImage imageNamed:@"coin_square0001"];
} else if (coin == COIN_CIRCLE){
return [UIImage imageNamed:@"coin_circle0001"];
}
return nil;
}
In Listing 4-24, we see the code that is executed when a coin was previously selected.
Listing 4-24. CoinsController.m (tapGesture:, continued)
if (CoordEqual(firstSelectedCoin, coinCoord)){//re selected the first one.
isFirstCoinSelected = false;
[self spinCoinAt:firstSelectedCoin];
} else {//selected another one, do switch.
acceptingInput = false;
[coinsGame setRemaingTurns: [coinsGame remaingTurns] - 1];
[delegate turnsRemainingDecreased:self with: [coinsGame remaingTurns]];
isFirstCoinSelected = false;
secondSelectedCoin = coinCoord;
[self stopCoinAt:secondSelectedCoin];
[self doSwitch:firstSelectedCoin With:secondSelectedCoin];
}