XUtils

flux

A fast, lightweight tweening library for Lua with easing functions and the ability to group tweens together.


flux

A fast, lightweight tweening library for Lua.

Additional options

Additional options when creating a tween can be set through the use of chained functions provided by the tween object which flux.to() returns.

flux.to(t, 4, { x = 10 }):ease("linear"):delay(1)

:ease(type)

The easing type which should be used by the tween; type should be a string containing the name of the easing to be used. The library provides the following easing types:

linear quadin quadout quadinout cubicin cubicout cubicinout quartin quartout quartinout quintin quintout quintinout expoin expoout expoinout sinein sineout sineinout circin circout circinout backin backout backinout elasticin elasticout elasticinout

The default easing type is quadout. Examples of the different easing types can be found here.

:delay(time)

The amount of time flux should wait before starting the tween; time should be a number of seconds. The default delay time is 0.

:onstart(fn)

Sets the function fn to be called when the tween starts (once the delay has finished). :onstart() can be called multiple times to add more than one function.

:onupdate(fn)

Sets the function fn to be called each frame the tween updates a value. onupdate() can be called multiple times to add more than one function.

:oncomplete(fn)

Sets the function fn to be called once the tween has finished and reached its destination values. oncomplete() can be called multiple times to add more than one function.

:after([obj,] time, vars)

Creates a new tween and chains it to the end of the existing tween; the chained tween will be called after the original one has finished. Any additional chained function used after :after() will effect the chained tween. There is no limit to how many times :after() can be used in a chain, allowing the creation of long tween sequences. If obj is not specified the obj argument from the original tween is used.

-- Tweens t.x to 10 over 2 seconds, then to 20 over 1 second
flux.to(t, 2, { x = 10 }):after(t, 1, { x = 20 })

Stopping a tween

If you want the ability to stop a tween before it has finished, the tween should be assigned to a variable when it is created.

local tween = flux.to(x, 2, { y = 20 }):delay(1)

The tween can then be stopped at any point by calling its :stop() method.

tween:stop()

This will cause the tween to immediatly be removed from its parent group and will leave its tweened variables at their current values. The tween’s oncomplete() callback is not called.

Groups

flux provides the ability to create tween groups; these are objects which can have tweens added to them, and who are in charge of updating and handling their contained tweens. A group is created by calling the flux.group() function.

group = flux.group()

Once a group is created it acts independently of the flux object, and must be updated each frame using its own update method.

group:update(deltatime)

To add a tween to a group, the group’s to() method should be used.

group:to(t, 3, { x = 10, y = 20 })

A good example of where groups are useful is for games where you may have a set of tweens which effect objects in the game world and which you want to pause when the game is paused. A group’s tweens can be paused by simply neglecting to call its update() method; when a group is destroyed its tweens are also destroyed.


Articles

  • coming soon...