Game Development Reference
In-Depth Information
batch.setColor(1, 1, 1, alpha);
batch.draw(nextScreen, 0, 0, 0, 0, w, h, 1, 1, 0, 0, 0,
nextScreen.getWidth(), nextScreen.getHeight(),
false, true);
batch.end();
}
}
First of all, notice how we were able to tuck away the render logic of our new
transition effect in such a nice and compact manner. The ScreenTransitionFade
class is built as a singleton so that it can be easily accessed from anywhere in the
code without having to create and keep multiple instances of it. The init() method
should be called before it is used. It takes one argument to define the duration of the
effect. The render() method uses both the supplied textures to create the desired
effect. It is achieved by first clearing the screen, followed by drawing the current
screen's texture, and lastly by drawing the next screen's texture on top of the other.
However, prior to the draw call of the next screen, we also change the drawing color.
It is set to full white, and the alpha channel, which controls the opacity for each of
the following draw calls, is set to alpha . The alpha variable contains the interpolated
value where we have chosen to use the fade algorithm.
Finally, let the menu screen use the fade transition effect when the player clicks on
the Play button. Add the following import lines to the MenuScreen class:
import com.packtpub.libgdx.canyonbunny.screens.transitions
.ScreenTransition;
import com.packtpub.libgdx.canyonbunny.screens.transitions
.ScreenTransitionFade;
After this, make the following changes to the same class:
private void onPlayClicked () {
ScreenTransition transition = ScreenTransitionFade.init(0.75f);
game.setScreen(new GameScreen(game), transition);
}
With these changes, the transition from the menu screen to the game screen will last
for 0.75 seconds or 750 milliseconds until finished. You can start the game now and
watch the transition live in action. Change the transition's duration to either slow
down or speed up the effect.
 
Search WWH ::




Custom Search