Game Development Reference
In-Depth Information
Listing 4-11. CoinsController.m (createAndLayoutImages)
-(void)createAndLayoutImages{
int rowCount = [coinsGame rowCount];
int colCount = [coinsGame colCount];
CGRect coinsFrame = [coinsView frame];
float width = coinsFrame.size.width/colCount;
float height = coinsFrame.size.height/rowCount;
for (int r=0;r<rowCount;r++){
for (int c=0;c<colCount;c++){
UIImageView* imageView = [[UIImageView alloc] init];
CGRect frame = NnCGRectMake(c*width, r*height, width, height);
[imageView setFrame:frame];
[coinsView addSubview: imageView];
[self spinCoinAt:CoordMake(r, c)];
}
}
frame of coinsView is stored in the variable coinsFrame for easy reference, and
width and height of each coin view is calculated. The two for loops create an imageView , set its
frame , and add it to coinsView for each row and column. In the nested loops, spinCoinAt: is called,
which creates the spinning coin effect. Note that the argument being passed to spinCoinAt: is the
result of the function CoordMake . We will take a look at this function and the class CoinsGame after
looking at spinCoinAt: , shown in Listing 4-12.
Listing 4-12. CoinsController.m (spinCoinAt:)
-(void)spinCoinAt:(Coord)coord{
UIImageView* coinView = [[coinsView subviews] objectAtIndex:[coinsGame indexForCoord:coord]];
NSNumber* coin = [coinsGame coinForCoord:coord];
NSArray* images = [imageSequences objectAtIndex:[coin intValue]];
[coinView setAnimationImages: images];
NSTimeInterval interval = (random()%4)/10.0+.6;
[coinView setAnimationDuration: interval];
[coinView startAnimating];
}
You can see that spinCoinAt: takes an argument of type Coord . This is defined in CoinsGame.h and
is a struct that represents a coin at a particular row and column. In the first line of this task, the
struct Coord is used to find the index of the UIView that represents this particular coin. This works
because there is exactly one UIImageView for each coin. (This was set up in Listing 4-11.) After the
correct UIImageView is found, we get the value of the coin from the coinsGame object. The NSNumber
coin represents the type of coin for this particular set of coordinates—either a triangle, square, or
circle. Using the intValue of the coin , we pull out an NSArray from imageSequences called images .
Search WWH ::




Custom Search