Transition Engine
Looking back at my last post about basic scenes you will see that I plan to implement a lot of basic functionality in my classic arcade game frame work. Another feature that I would like to implement is a simple transition engine.
What is a transition engine?
When you change from one scene to another in a Pulpcore applet it does a straight out swap between the two scenes. The old scene is gone and the new scene is shown. It would be nice to do some thing a little bit more fancy, say a side swipe, color fade or other nifty transition effect. This is where the transition engine would come in.
My basic design is to make a new scene called the transition scene. The transition scene would take a parameter of Itransition. Itransition would be an interface that individual classes could implement to allow them to be used as a transition.
In my current prototype I have implemented a very simple transition engine in this manner. My transition interface look like the following:
public interface ITransition { void initialize(Scene2D scene); boolean update(int elapsedTime); }
As you can see the interface is pretty simple. There are only two method on the interface. And initialize that takes a scene object. This is so the transition can add sprites and groups to the current scene so it is able to draw them. The other is an update function. This lets the transition be called every game loop to update the transitions progress, when the transition is complete it can signal this by returning a false flag.
The transition scene is also quite simple.
public class TransitionScene extends Scene2D { private ITransition transition; private Scene nextScene; public TransitionScene(ITransition transition, Scene nextScene) { this.transition = transition; this.nextScene = nextScene; transition.initialize(this); } public void update(int elapsedTime) { if(!transition.update(elapsedTime)) { Stage.setScene(nextScene); } } }
So you can see that the transition scene takes and instance of an object that implements the Itransition interface and then every game loop tells the transition to update it's self and then inform it if the transition is complete. When the transition is complete it then sets the stage to the new scene.
What would an implemented transition look like?
A transition could look like any thing as long as it implements the Itransition interface. I believe this will make it flexible enough to create nearly any transition effect I can think of.
I currently have a single transition effect, it is a fade the current scene to a color effect.
public class FadeToColorTransition implements ITransition { private FilledSprite background; private ImageSprite foreground; private int duration; public FadeToColorTransition(CoreImage image, int color, int duration) { background = new FilledSprite(color); foreground = new ImageSprite(image, 0, 0); this.duration = duration; } public void initialize(Scene2D scene) { scene.add(background); scene.add(foreground); foreground.alpha.animate(255, 0, duration); } public boolean update(int elapsedTime) { boolean continueTransition = true; if(foreground.alpha.get() == 0) { continueTransition = false; } return continueTransition; } }
So from the code you can see the transition effect is quite short but still pretty powerful.
It takes the following parameters on construction. A CoreImage object which is generally a screen shot of the scene you are transitioning away from, a color to fade the image into and a duration in milliseconds for the fade to take place over. The transition then animates the alpha of the image to fade out over time and blends it with the colored background to generate the fade effect. When the image alpha is at 0 the effect is over and the transition returns a false.
An beneficial side effect of implementing the transition effects in this way is that you can make as many of them as you want. When you run the Ant build script in release mode it will run the Progaurd build action. One of the things Progaurd does is strip out any unused code so only the transitions that you actually use will make it the end jar file. This results in a smaller jar, less time spent waiting for the jar file to download and a better user experience when the player wants to try your game.
Related Posts:
Basic Scenes
In Pulpcore applets are made up of a collection of scenes, scenes are where you...