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/4.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 frequency in your game.project. It's 60
by default.
Add next tweener
section to your game.project
in text mode:
[tweener]
update_frequency = 60
-- Tween function can be a string, a predefined easing function, or a custom easing function
local tween_function = "linear" or tweener.linear or go.EASING_LINEAR or gui.EASING_LINEAR or {0, 0.2, 0.4, 0.8, 0.9, 1}
tweener.ease(tween_function, from, to, time, time_elapsed)
tweener.tween(tween_function, from, to, time, callback, [dt]) -- Returns tween_id
tweener.cancel(tween_id)
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:
tweener_id
: A tweener 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)
local tween_id = 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)
-- You can cancel the tween by calling tweener.cancel
tweener.cancel(tween_id)
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)
tweener.cancel(tweener_id)
This function cancels the tween with the given tweener_id
. This calls timer.cancel
internally.
-
Parameters:
tweener_id
: The id of the tween to cancel.
-
Return Value:
true
if the tween was successfully canceled,false
otherwise.
-
Usage Example:
local tween_id = tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.cancel(tween_id)
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.
- Initial release
- Changed timer
delta time
tosocket.gettime
for more precise tweening
- Added custom easings support
- Fix
update_frequency
game.project settings typo - Now able to use string name of easing functions (ex. "linear", "insine", "outquad", etc.)
- Add
tweener.cancel
function to cancel tweening instead oftimer.cancel
(For better API and README) - Code cleanup and better performance
- Fix if total time is 0, then callback will be called immediately
Your donation helps me stay engaged in creating valuable projects for Defold. If you appreciate what I'm doing, please consider supporting me!