Rich Animations with Fluent Effects
With all my work on Shade I needed to create a number of animated effects. Naturally, I started by creating an object to address my needs, then I copied its functionality over and tweaked it, then I copied it again and tweaked it further. After the third time I realized that there should be a better way to create rich visual effects without having to reinvent the wheel each time. In the end, I wrote a small library to consolidate this behavior and simplify the creation of compelling effects, introducing Fluent Effects
Demo
Anyway, let’s cut to the chase. Here’s an example showing off how you might use Fluent Effects in a game:
Downloads
For those of you who just want to dig in here’s the code:
Explanation
So what’s this all about? I started using David’s excellent SlickFx library. It works great but I found it to require a lot of boilerplate to do simple things. For instance, in Shade I have a number of types of text which simply fade in or out. To accomplish this with SlickFx you need to implement the AlphaEntity interface, configure an effect, and create a timeline. Furthermore, it requires working with images, which isn’t always desirable, for instance when working with fading score text.
So, with all that in mind I set about creating an interface that I would want to work with. There were a couple high level requirements:
- Create compelling user experiences with rich effects
- Concise and intuitive interface
- Doesn’t force you to inherit or implement anything
- Animate more than one property over the timeline
- Chain actions together to create complex animations
- No dependencies, runs on Java 1.5
To accomplish the above I opted to implement a fluent interface. This lets you configure your animations like so:
// play forward, wait one second, play backwards, loop three times
fx.forward().halt(1000).reverse().loop(3);
I’ve also created an annotation which allows you to concisely describe how a given property should be animated. For instance, you might try:
// alpha will go from zero to one over a second
@FluentProperty(start = 0, stop = 1, duration = 1000)
public void setAlpha(float a) { }
Of course, sometimes it’s not convenient to annotate your properties or you want to have multiple timelines for a single objects. In those cases you can configure the timeline programmatically:
// alpha will go from zero to one over a second
FluentConfig c = new FluentConfig();
c.add("setAlpha", 0, 1, 1000);
Finally, you put everything together like so:
// create a new timeline for the given object
Timeline fx = new Timeline(object);
// play forward, wait one second, play backwards, loop
fx.forward().halt(1000).backward().loop();
// start the animation
fx.start();
// advance the timeline some number of steps
fx.step(delta);
Conclusion
That completes the five minute tour. Please let me know if you find this useful or would like to see the full source code for the examples. Finally, if you have any suggestions for how to make the library better please let me know in the comments!
comments
-
gilley55
-
Held
-
aschearer
-
kev
-
aschearer
-
Gornova
-
aschearer








