Tween
Introduction
Tween enables changing any numerical property on an object over a given amount of time using a variety of interpolation types. Tween is an extension method on the PositionedObject type, so it works on any entity and most objects that are added to entities such as Sprites and collision shapes.
For information on performing tweening with objects which do not implement PositionedObject, see the TweenerManager.TweenAsync method.
Code Example - Move Circle X
The following code shows how to tween the location of a Circle to the edge of the screen when pressing either the left or right keys. This example assumes:
You have a
ScreenThe Screen has a
Circleobject. For this example theCircleis namedCircleInstance.
To use Tween to change the position of the Circle based off of keyboard input:
Add the following using statement:
using StateInterpolationPlugin;Add the following to
CustomActivity:
if (Keyboard.Main.KeyPushed(Keys.Left))
{
CircleInstance.Tween(
property: "X",
to: Camera.Main.AbsoluteLeftXEdgeAt(0),
during: 1,
interpolation: FlatRedBall.Glue.StateInterpolation.InterpolationType.Bounce,
easing: FlatRedBall.Glue.StateInterpolation.Easing.Out
);
}
if (Keyboard.Main.KeyPushed(Keys.Right))
{
CircleInstance.Tween(
property: "X",
to: Camera.Main.AbsoluteRightXEdgeAt(0),
during: During(1),
interpolation: FlatRedBall.Glue.StateInterpolation.InterpolationType.Bounce,
easing: FlatRedBall.Glue.StateInterpolation.Easing.Out
);
}Code Example - Zoom Camera with Delegates
If your Camera is 2D (default, Orthogonal = true), then zooming requires modifying two values:
OrthogonalHeightOrthogonalWidth(usually by callingFixAspectRatioYConstant)
Rather than creating two Tween functions that run in parallel, the Tween function allows using a delegate to assign multiple values based on a single value. The example below uses the OrthogonalHeight as the main value, and adjusts the OrthogonalWidth by calling FixAspectRatioYConstant.

Code Example - Lambda Assignments
Lambdas can be used to assign properties without creating dedicated functions. For example a Circle's Radius can be set using the following code:

TweenAsync
TweenAsync returns a Task which can be awaited to perform logic after the tweening has finished. For example, multiple TweenAsync methods can be combined to create a series of tweens.
The following code shows how to move a Circle in response to the space bar being pressed. The Circle moves to the right with a bounce interpolation, moves to the left with another bounce interpolation, then finally returns to its original position using elastic interpolation.

Tweening vs. Velocity values
The tweening logic performed internally by calling Tween is executed regardless of the variable type. That is, the variable does not need an accompanying velocity variable (such as X and XVelocity) to tween properly. Furthermore, since the tweening occurs in the TweenerManager's update, the owner of the variable that is being changed does not need to be automatically updated.
Last updated
Was this helpful?