@kidkiedis and @vergil12345678
go.animate() does not block code execution when called. It will start the animation but your Lua code will immediately execute the next line.
This means that any for loop where you call go.animate() in every iteration will basically call go.animate() 10 times in a row without waiting for the animation to finish.
This means that you can’t use a for-loop *
We need some other construct if we want the animation to go through each point in the path. The go.animate() function has a convenient callback function that we can use for this purpose. We start the first animation to the first step through the path. When we get the callback we take the next step, until the path has reached its end. Something like this:
local take_step = nil
take_step = function(self)
self.step = self.step + 1
if self.step > #self.path then
return
end
moveto = self.path[self.step].coords
go.animate ('.', "position", go.PLAYBACK_ONCE_FORWARD, moveto, go.EASING_LINEAR, speed, duration, take_step)
end
function init(self)
self.path = {}
self.step = 0
-- calculate path somehow
take_step(self)
end
Or if we’re allowed to remove steps from the path:
local take_step = nil
take_step = function(self)
local p = table.remove(self.path, 1)
if not p then
return
end
moveto = p.coords
go.animate ('.', "position", go.PLAYBACK_ONCE_FORWARD, moveto, go.EASING_LINEAR, speed, duration, take_step)
end
function init(self)
self.path = {}
-- calculate path somehow
take_step(self)
end
*=You can if you use a coroutine but let’s ignore that for now