Game Development Reference
In-Depth Information
//make changes
[welcomeView setAlpha:0.0];
[highscoreController.view setAlpha:1.0];
[UIView commitAnimations];
}
The task shown in Listing 4-5 is called when the user touches the Highscores button in the Home
view. To create the fade effect, we utilize the static animation task from the class
UIView
, but first we
have to set up a few things. We start by setting the
alpha
property of the view associated with the
highscoreController
to 0.0, or completely translucent. We then add the High Score view to the root
view. This places it onto the already present
welcomeView
, but because it is clear, we see only the
Welcome screen.
The animation is set up by calling the task
beginAnimations:context:
, and the duration is set with
the task
setAnimationDuration:
. The next step is to set a delegate for the animation that is being
created. We also want to set which task should be called when the animation is complete by calling
setAnimationDidStopSelector:
. After the animation is set up, we simply set the
alpha
property of
welcomeView to 0.0 and set the High Score view's
alpha
to 1.0. This indicates that at the end of the
animation, we want the Welcome view to be transparent and the High Score view to be fully opaque.
We then indicate that we have set our finished state by calling
commitAnimations
.
We set self to be the delegate for the animation and set up the task
animationDidStop:finished:
context:
to be called when we want the animation to be over. We did this so we could do a little
cleanup at the end of the animation and ensure that our application is in the correct state. Listing 4-6
shows the implementation of this callback method.
Listing 4-6. GameController.m (animationDidStop:finished:context:)
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished
context:(void *)context{
if ([@"fadeInHighScoreView" isEqual:context]){
[welcomeView removeFromSuperview];
[welcomeView setAlpha:1.0];
}
}
The task in Listing 4-6 is called at the end of the fade animation. In this task, we want to remove
welcomeView
from the scene and reset its
alpha
to 1.0. We remove it, because that is the expected
state for the other transitions. Setting the
alpha
back to 1.0 ensures that
welcomeView
is visible when
it is added back into the scene.
You have seen how a view is added to a super view by using the property
frame
. You have also
learned how to create animations that affect the properties of a
UIView
by exploring the fade
transition. We are going to combine these concepts to show you how to animate the coins within
the game, but first you have to take look at how we set up the related classes so that this all
makes sense.