#
The Fade TransitionThe first transition effect we'll create is going to be the most command and simplest one of all, a fade effect. This transition will simply fade out the current scene and then fade in the next one.
Perform the Following
In the game project, reate a new class file called FadeTransition.cs, then add the following code. We'll go over the code in the sections below.
using statements
FadeTransition class
#
ConstructorThe constructor of our FadeTransition
is pretty simple.
It simply just takes in the required values of the base Transition
constructor and then passes them along.
#
Render(SpriteBatch)The Render(SpriteBatch)
method is rather simple as well.
All it does is render the source texture, but sets the color mask to Color.White * GetAlpha()
. By multiplying it by an alpha value, we can make the render of the texture transparent depending on the amount of alpha we apply.
#
GetAlpha()The GetAlpha()
method is responsible for determining how much alpha to apply to the render.
Depending on if the fade transition is fading in or out, we interpolate the alpha value between 0.0 and 1.0 based on the amount of time that is remaining for the transition.
#
Testing The TransitionNow let's test the transition.
Perform the Following
Open the GreenCircleScene.cs class file and locate the Update(GameTime)
method. Change the method to the following.
We've updated it so that when we call _game.ChangeScene
we are now using the (Scene, Transition Transition)
overload. We pass to it a new OrangeCircleScene
instance, a FadeTransition
instance that is fading out and a FadeTransition
instance that is fading in.
Perform the Following
Next, open the OrangeCircleScene.cs class file and make the same change to it's Update(GameTime)
method as we did above to. Only for this one, make sure we are changing to the GreenCircleScene
instead.
If you run the game now, you should see the green circle scene first. Then if you press the space key to change scenes, the green circle scene will fade out, then the orange circle scene will fade in. It's magic!!!
The fade transition effect is nice and all, but we can create something even fancier. In the next page of this tutorial, we'll create the even-odd tile transition effect.