Repeating Path Following

So I’ve looked at:

https://coronalabs.com/blog/2014/01/07/tutorial-moving-objects-along-a-path/

and I downloaded the code, all I really want to do is for the object to continue following the path forever. I don’t know why but my brain has ran out of ways to do this today. I was thinking about having a boolean flag and a while loop per:

while (repeating == true) do for i = 1,#path do local segmentTime = 500 --if "constant" is defined, refactor transition time based on distance between points if ( constant ) then local dist if ( i == 1 ) then dist = distBetween( object.x, object.y, deltaX+path[i].x, deltaY+path[i].y ) else dist = distBetween( path[i-1].x, path[i-1].y, path[i].x, path[i].y ) end segmentTime = dist\*speedFactor else --if this path segment has a custom time, use it if ( path[i].time ) then segmentTime = path[i].time end end --if this segment has custom easing, override the default method (if any) if ( path[i].easingMethod ) then ease = path[i].easingMethod end transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease } ) delay = delay + segmentTime end end

but that very quickly causes a crash. Open to any suggestions, I’m outta ideas. 

I see ‘repeating’ is not used in this code other than at the top, so that will cause it to crash; The loop never ends.

Why not have this loop execute within a function called ’ makeMotionLoop ()’ and remove the ‘while (repeating == true) do’ loop.

Then change the transition.to into this:

local repeatFunc = makeMotionLoop if (i \< #path) then repeatFunc=nil end transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=repeatFunc } )

What I see you’re doing in your loop is create a bunch of transitions all at once, each delayed long enough to let the transitions which run before it complete.

What this code change does is simply builds that list of transitions again once the last one has finished.

I see ‘repeating’ is not used in this code other than at the top, so that will cause it to crash; The loop never ends.

Why not have this loop execute within a function called ’ makeMotionLoop ()’ and remove the ‘while (repeating == true) do’ loop.

Then change the transition.to into this:

local repeatFunc = makeMotionLoop if (i \< #path) then repeatFunc=nil end transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=repeatFunc } )

What I see you’re doing in your loop is create a bunch of transitions all at once, each delayed long enough to let the transitions which run before it complete.

What this code change does is simply builds that list of transitions again once the last one has finished.