Tweening
⚠️ Advanced Topic
This section is intended for students who are comfortable with the basics of object-oriented programming. You should already know how to define classes, create objects, and work with object properties. If you are new to OOP, we recommend you to stick to using the methods mentioned in the interpolation section.
Tweening, short for in-betweening, is a way to animate objects smoothly over time. Instead of manually updating positions, sizes, or other properties every frame, a Tween allows you to define a start value, an end value, a duration, and optionally an easing (how the value changes over time). The Tween system then handles all the intermediate steps for you.
Tweening is closely related to interpolation, which you’ve already learned about. Interpolation is the calculation of values between two points. Tweening uses interpolation to gradually change an object’s properties from one value to another over time.
Our implementation of Tweening is inspired by GSAP (GreenSock Animation Platform), a popular animation library for web development. GSAP allows you to animate objects with a simple, fluent, chainable syntax. We mimic that style in our game engine to make animations intuitive and easy to read.
You can also create your own animations with interpolation, but when you want to animate multiple properties at the same time (e.g. x, y and scale), then tweening is going to be more efficient (and easier to write).
Tweening gives you:
- Cleaner Code – No need for manual frame-by-frame calculations.
- Smooth Animations – Uses easing curves for realistic movement.
- Reusability – Works for any object and any numeric property.
Tweening by example
Our tweening implementation only works on classes. So it is important that you create classes for you own game objects. The properties of a defined class can be animated, as longs as they are of datatype float.
For example, the following Dice class can be animated:
public class Dice {
public float x;
public float y;
public float size;
public Dice(float x, float y, float size) {
this.x = x;
this.y = y;
this.size = size;
}
}
Let's first show how a tween can be created. After that we explain all of the methods seperately. We could then animate our dice in our screen as follows:
public class ExampleScreen extends ScalableScalableGameScreen {
// This is the object that we are animating (x=0, y=0, size=200)
Dice playerDice = new Dice(0, 0, 200);
// This is where we will store our tween
Tween exampleTween;
@Override
public void show() {
// Here we define the animation.
exampleTween = Tween.on(playerDice)
.to("x", 300)
.to("y", 150)
.to("size", 120)
.duration(1f)
.ease(Interpolation.pow2Out);
}
@Override
public void render(float delta) {
super.render(delta);
// Calculate the new values (this will update the properties of the dice)
exampleTween.update(delta);
// When the animation has finished, reverse the animation
if (exampleTween.isFinished()) {
exampleTween.reset(true);
}
// Draw the dice
GameApp.clearScreen();
GameApp.drawRectCentered(playerDice.x, playerDice.y, playerDice.size, playerDice.size, "white");
}
...
}
This will animate playerDice over 1 second, moving it to (300, 150), resizing it to 120. After the tween has finished, the tween will be reversed and played again.
Methods
The Tween class allows you to animate properties of objects over time. It uses a fluent API inspired by GSAP, meaning you can chain method calls in a readable way.
Selecting an Object
You can create a Tween object by calling Tween.on() and passing the object that you want to animate.
Animating Properties
Use to(string property, float newValue) to animate a property from its current value to a new value. The property must be a float field in your object.
Duration
With duration(float durationInSeconds) you will set how long the Tween will take to reach the target values, in seconds.
Easing / Interpolation
Chooses how the values change over time. For example, pow2Out starts fast and slows down at the end. If you don’t set an easing, the default is linear. These interpolation methods are identical to the ones mentioned in the Interpolation article.
Delay (optional)
Waits a given number of seconds before starting the Tween.
Callback on complete (optional)
Sometimes you want to get a notification when the animation is finished. This can be done by polling (see isFinished() method). It is also possible to work with events (just like with input handling).
Update(float delta)
During the render method we will need to call the update(float delta) method, to let the tween calculate the next value for all properties you are animating.
Finished
With the isFinished() method we can determine if the tween has finished.
Notes
- To work with tweens, always store properties in classes, not in local variables.
- Use
on(object)to select what you want to animate. - Use
to(property, value)to define which properties to animate. - Tweens handle all intermediate steps automatically.