This will loop through all 10 steps immediately and make 10 go.animate() calls immediately. It will however only complete the 10th go.animate() call. The rest of them should be ignored.
I assume what you really want is to move to the next step when the first animation has completed?
Store a step counter somewhere and increment and get next moveto value when the go.animate() callback is invoked.
local step = 0
local changeDir
changeDir = function()
if step < NUMBER_OF_STEPS then
go.animate(..., changeDir) --call itself in go.animate callback
step = step + 1
end
end
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
These are not equivalent. The length operator returns the length of the array part of a table. The length is the number of sequential keys from 1 and upwards.
table.maxn() gives the highest numerical index in your table.
Note that while the length operator returns a length of 6 you will get into trouble if iterating using ipairs() which will stop at the first nil value (ie key 5)