Game Development Reference
In-Depth Information
For each
UIView
,
coinViewA
and
coinViewB
, we will be creating two
CABasicAnimation
objects. When
we create the
CABasicAnimation
object
animScaleDown
, we specify that this
CABasicAnimation
will be
manipulating the value of
scale
on the property
transform
. We specify the starting value of the
scale
by setting the
fromValue
of
animScaleDown
, and we specify the ending value by setting the
toValue
.
The value duration indicates how many seconds we want this animation to take. By specifying the
timingFunction
, we control the rate at which this animation will take place. In this case, we indicate
kCAMediaTimingFunctionEaseIn
, which tells the
CABasicAnimation
to start out slow and then speed up.
The
CABasicAnimation animScaleUp
is similar to
animScaleDown
. The big difference is that the
fromValue
and
toValue
are opposite. We also set the value for
beginTime
to 1 second in the
future. The idea here is that we want the animation
animScaleDown
to run for a second, making
the coin get small enough to vanish, and then we want the animation
animScaleUp
to run when
animScaleDown
is done, scaling the coin back up. The trick will be to swap the images used by the
delegate
to
animScaleDown
, if we are working with
coinViewA
, because we need to be notified
self
as
delegate
to
animScaleUp
. The animations that have had their delegates set will call the task
when they are done, as shown in Listing 4-26.
CoinsController (animationDidStop:finished:)
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
if ([[theAnimation valueForKey:@"name"] isEqual:@"animScaleDown"]){
UIImage* imageA = [coinViewA image];
[coinViewA setImage:[coinViewB image]];
[coinViewB setImage:imageA];
} else if ([[theAnimation valueForKey:@"name"] isEqual:@"animScaleUp"]){
[self checkMatches];
[self spinCoinAt:firstSelectedCoin];
[self spinCoinAt:secondSelectedCoin];
} else if ([[theAnimation valueForKey:@"name"] isEqual:@"animateOffScreen"]){
[coinsGame randomizeRows: matchingRows];
[coinsGame randomizeCols: matchingCols];
[self updateCoinViews];
}
}
Here we can see the task
animationDidStop:finished:
. Notice that three animations call this task.
The first two if statements are for handling the animations described in Listing 4-25. When the
animScaleDown
animation is done, we swap the
UIImages
used by
UIImageViews
to represent the two
selected coins.
When the animation
animScaleUp
is done, we want to check whether there are any matches. Calling
checkMatches
does this. We also want to start the recently selected coins spinning again. The
implementation of
checkMatches
is shown in Listing 4-27.