Game Development Reference
In-Depth Information
Coord coord = CoordMake(r, [col intValue]);
int index = [coinsGame indexForCoord:coord];
UIImageView* coinView = [[coinsView subviews] objectAtIndex: index];
if (!isDelegateSet && r == 0){
[animateOffScreen setDelegate:self];
}
[coinView.layer addAnimation:animateOffScreen forKey:@"animateOffScreenY"];
}
}
}
int totalMatches = [matchingCols count] + [matchingRows count];
if (totalMatches > 0){
[coinsGame setScore:[coinsGame score] + totalMatches];
[delegate scoreIncreases:self with:[coinsGame score]];
} else {
if ([coinsGame remaingTurns] <= 0){
//delay calling gameOver on the delegate so the coin's
[NSTimer scheduledTimerWithTimeInterval:1 target:self
} else {
//all matches are done animating and we have turns left.
acceptingInput = YES;
}
}
We find all matching rows and columns by calling
findMatchingRows
and
findMatchinCols
on
coinsGame
. After we have these arrays of matches, we want to animate all of the coins involved in
a match of the screen. If there are
matchingRows
, we create a new
CABasicAnimation
for each coin
that modifies the position value of x of the underlying
CALayer
object. Instead of setting the exact
starting and ending values of x, we can simply specify the
byValue
, which we make the
width
of the
coinsView
. By specifying the
byValue
, we can use this animation for all coins in the row, because
this animation will simply infer that the
fromValue
is the starting
x
value for each coin view, and the
toValue
with be
fromValue
+
byValue
.
After the animations for matching rows and columns are created, we check to see how many
matches in total were created on this turn. If this value is bigger than zero, we update the score
of the
coinsGame
object and inform the
delegate
that the score has changed. If no matches are
found, we check whether the game is over. If it is over, we use the
NSTimer
class to call
doEndGame
in
1 second. This delay is for aesthetic reasons; it is nice to have a little pause before the high scores
are shown. If it is not the end of the game, we simply start accepting input again. Listing 4-28 shows
the task
doEndGame
, which simply informs the delegate that the game is over.