Tweener - is a single file Lua module for the Defold game engine. It provides a way to handle manual tweening in your game.
- Tweening: Create tweens for any action your want.
- Easing Functions: Provides a set of easing functions for different types of easings.
- Custom Update Frequency: Option to define update frequency for the tween.
- Callbacks: Callbacks for each tween update.
- Custom Easings: Support for custom easing functions.
Open your game.project
file and add the following line to the dependencies field under the project section:
https://github.com/Insality/defold-tweener/archive/refs/tags/3.zip
Note: The library size is calculated based on the build report per platform
Platform | Library Size |
---|---|
HTML5 | 3.28 KB |
Desktop / Mobile | 6.21 KB |
Optionally, you can setup global default update freqency in your game.project. It's 60
by default.
Add next tweener
section to your game.project
in text mode:
[tweener]
update_frequency = 60
local tween_function = tweener.linear or go.EASING_LINEAR or gui.EASING_LINEAR or {0, 0.2, 0.4, 0.8, 0.9, 1}
tweener.tween(tween_function, from, to, time, callback, [dt])
tweener.ease(tween_function, from, to, time, time_elapsed)
To start using the Tweener module in your project, you first need to import it. This can be done with the following line of code:
local tweener = require("tweener.tweener")
The Tweener module provides two primary functions to work with tweens:
tweener.tween(tween_function, from, to, time, callback, [dt])
This function initiates a tween operation immediately. Here's how to use it:
-
Parameters:
tween_function
: The tween function. You can use one of the predefined easing functions or provide a custom one.from
: The starting value of the tween.to
: The ending value of the tween.time
: The duration of the tween, in seconds.callback
: Afunction(value, is_final_call)
that gets called upon each update of the tween.dt
(optional): The time interval for updating the tween, in seconds.
-
Return Value:
timer_id
: A timer id fromtimer.delay
if you wish to cancel the tween.
-
Usage Example:
tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.tween(go.EASING_OUTSINE, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.tween({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.ease(tween_function, from, to, time, time_elapsed)
This function calculates the value of the tween at a specific point in time, based on the easing function provided.
-
Parameters:
tween_function
: The tween function.from
: The starting value of the tween.to
: The ending value of the tween.time
: The total duration of the tween, in seconds.time_elapsed
: The elapsed time since the start of the tween, in seconds.
-
Usage Example:
local value = tweener.ease(tweener.inquad, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)
local value = tweener.ease(gui.EASING_OUTSINE, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)
local value = tweener.ease({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)
These functions provide a flexible and powerful way to add animations and transitions to your projects, making them feel more dynamic and engaging. Enjoy animating with the Tweener module! (Thanks, Mister ChatGPT) 🙃
You can use Tweener to animate scoring text, for example:
This animation can be created using the following code:
tweener.tween(gui.EASING_OUTCIRC, 0, 9999, 2.4, function(value, is_final_call)
gui.set_text(text_score, "Score: " .. math.floor(value))
if is_final_call then
gui.set_scale(text_score, vmath.vector3(1.25, 1.25, 1))
gui.animate(text_score, "scale", vmath.vector3(1, 1, 1), gui.EASING_OUTBOUNCE, 0.5)
end
end)
You can obtain the value of the tween at any point in time with the tweener.ease
function:
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0) -- Returns 0
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.25) -- Returns 38.268343236509
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.5) -- Returns 70.710678118655
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.75) -- Returns 92.387953251129
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 1) -- Returns 100
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any issues, questions or suggestions please create an issue.
Changelog
- Initial release
Changelog
- Changed timer `delta time` to `socket.gettime` for more precise tweening
Changelog
- Added custom easings support
Your donation helps me stay engaged in creating valuable projects for Defold. If you appreciate what I'm doing, please consider supporting me!