Do I have to chain animations on the same property using the callback function or coroutines?
This does not seem to produce the effect I would expect:
gui.animate(self.combo_start, "position.x", 320, gui.EASING_INOUTQUART, 0.5)
gui.animate(self.combo_start, "position.x", -960, gui.EASING_INOUTQUART, 0.5, 1.5)
expected: node moves to position.x 320, waits 1.5 secs and then moves to position.x -960
result: nodes waits 1.5 secs and then moves to position.x -960
I am not sure but I would say that the second animation cancel the first one
(and in any case, the second animation wait only 1.0 since the first one has to complete).
The following code do what you want:
gui.animate(self.combo_start, “position.x”, 320, gui.EASING_INOUTQUART, 0.5, 0.0,
function()
gui.animate(self.combo_start, “position.x”, -960, gui.EASING_INOUTQUART, 0.5)
end)
@roccosaienz is correct. What happens is that you start one animation and immediately after start another animation which cancels the first.
Here’s a Lua module that will help you do it: ludobits/ludobits/m/sequence.lua at master · britzl/ludobits · GitHub
Usage:
local sequence = require "sequence"
sequence.run_once(function()
sequence.gui_animate(self.combo_start, "position.x", 320, gui.EASING_INOUTQUART, 0.5)
sequence.gui_animate(self.combo_start, "position.x", -960, gui.EASING_INOUTQUART, 0.5, 1.5)
end)
3 Likes
Here’s the documentation:
If the node property is already being animated, that animation will be canceled and replaced by the new one
1 Like
right! Thanks everyone, this makes sense
Ludobits seems like a great little module, does a lot of things I find really useful. Bookmarked!
Yes there’s a bunch of stuff in there, some good, some bad. It’s a place where I experiment with various solutions to Defold problems. Some things make it into their own official Defold projects.
1 Like
Pkeod
May 4, 2020, 2:28pm
8
I use this solution for chaining GUI animations
And if you want to chain many you could leverage coroutines to avoid very deeply nested chains of callbacks:
local function gui_animate(node, prop, to, easing, duration, delay, playback)
local co = coroutine.running()
assert(co, "Only call this from within a coroutine")
gui.animate(node, prop, to, easing, duration, delay, function()
-- resume the coroutine now that the animation is finished
coroutine.resume(co)
end, playback)
-- pause the coroutine until resumed (when animation is fini…
2 Likes