Continuous Movement of an Object

I want to have an object move along a continuous predefined line.  The line may be around another object or just across the game board.  I thought I say a tutorial for this some time ago but I am not able to find it now.

Does anyone have any suggested code for how to do this OR does anyone know if there is a tutorial on this?

Thanks,

Lori

Perhaps this is what you were looking for:   http://coronalabs.com/blog/2014/01/07/tutorial-moving-objects-along-a-path/

Rob

That is the one I was looking for.  Thank you!

I set up my object to move the same as in the tutorial.    My object has four basic lines I want it to follow and then I want it to repeat that same four lines continuously during the level being played.

Is there a way I can make it repeat without having to input a zillion movePath[#] rows?

I’ve been trying various things but nothing is working for me.

Thanks

I would put an onComplete to restart it.

Rob

Where do I send it to restart?  I tried onComplete = restart and onComplete = setPath 

Thanks

Lori

Generally you probably should have your transitions inside a function, perhaps something like this:

local function moveIt()

    … your transition code here:

    transition.to(…, {…, onComplete = moveIt} )

end

  Below is the code I’m using:  When I added the function (see blue) the transition did not start - not even one rotation.  I know this is basic but I’m not getting it.  Thanks

local movePath = {}

movePath[1] = { x=680, y=253, time=8000 }

movePath[2] = { x=680, y=840, time=8000 }

movePath[3] = { x=105, y=840, time=8000 }

movePath[4] = { x=105, y=253, time=8000 }

movePath[5] = { x=384, y=253, time=8000 }

local function distBetween( x1, y1, x2, y2 )

local xFactor = x2 - x1

local yFactor = y2 - y1

local dist = math.sqrt((xFactor*xFactor) + (yFactor*yFactor))

return dist

end

local function setPath( object, path, params )

local delta = params.useDelta or nil 

local deltaX = 0

local deltaY = 0

local constant = params.constantTime or nil

local ease = params.easingMethod or easing.linear

local tag = params.tag or nil

local delay = params.delay or 0

local speedFactor = 1

if ( delta and delta == false ) then

–if delta = true, the object’s path will be relative to its starting position

–if dealta = false or ommitted, the path points will be explicit screen positions

deltaX = object.x

deltaY = object.y

end

if ( constant ) then

–this sets a constant rate of movement

local dist = distBetween( object.x, object.y, deltaX+path[1].x, deltaY+path[1].y )

speedFactor = constant/dist

end

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

function moveIt()

moveHole =  transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=moveIt** } )**

delay = delay + segmentTime

end

end

end

setPath(pocket1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag=“moveObject” } )

setPath(pocketSensor1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag=“moveObject” } )

Can you repost your code inside of and tags (no space after the [).   This will make it easier to read your code.

Rob

OK…but I’m not sure what you are asking.  Is it a different place I upload the code?   

Thanks

How are you putting your code in your post now?

cut and paste from TexMate

Then type:  into the forum screen (leave the space out, I can't type it without it turning into the tag), paste your code and then type:  (again leave the space out).

That will cause it to preserve your indentions and provide syntax highlighting, etc.  It’s important formatting for people trying to help you.

Rob

  local movePath = {} movePath[1] = { x=680, y=253, time=8000 } movePath[2] = { x=680, y=840, time=8000 } movePath[3] = { x=105, y=840, time=8000 } movePath[4] = { x=105, y=253, time=8000 } movePath[5] = { x=384, y=253, time=8000 }     local function distBetween( x1, y1, x2, y2 ) local xFactor = x2 - x1 local yFactor = y2 - y1 local dist = math.sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end     local function setPath( object, path, params )   local delta = params.useDelta or nil  local deltaX = 0 local deltaY = 0 local constant = params.constantTime or nil local ease = params.easingMethod or easing.linear local tag = params.tag or nil local delay = params.delay or 0 local speedFactor = 1   if ( delta and delta == false ) then --if delta = true, the object's path will be relative to its starting position --if dealta = false or ommitted, the path points will be explicit screen positions deltaX = object.x deltaY = object.y end   if ( constant ) then --this sets a constant rate of movement local dist = distBetween( object.x, object.y, deltaX+path[1].x, deltaY+path[1].y ) speedFactor = constant/dist end   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     moveHole =  transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=moveIt}) delay = delay + segmentTime   end   end   setPath(pocket1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } ) setPath(pocketSensor1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } )

I hope that was right

It still lost the indentions…

 I will try again.  If it still doesn’t work - I will post again and manually put in the indentions.  Thanks for your patience.

local movePath = {} movePath[1] = { x=680, y=253, time=8000 } movePath[2] = { x=680, y=840, time=8000 } movePath[3] = { x=105, y=840, time=8000 } movePath[4] = { x=105, y=253, time=8000 } movePath[5] = { x=384, y=253, time=8000 }     local function distBetween( x1, y1, x2, y2 ) local xFactor = x2 - x1 local yFactor = y2 - y1 local dist = math.sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end     local function setPath( object, path, params )   local delta = params.useDelta or nil  local deltaX = 0 local deltaY = 0 local constant = params.constantTime or nil local ease = params.easingMethod or easing.linear local tag = params.tag or nil local delay = params.delay or 0 local speedFactor = 1   if ( delta and delta == false ) then --if delta = true, the object's path will be relative to its starting position --if dealta = false or ommitted, the path points will be explicit screen positions deltaX = object.x deltaY = object.y end   if ( constant ) then --this sets a constant rate of movement local dist = distBetween( object.x, object.y, deltaX+path[1].x, deltaY+path[1].y ) speedFactor = constant/dist end   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     moveHole =  transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=moveIt}) delay = delay + segmentTime   end   end   setPath(pocket1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } ) setPath(pocketSensor1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } )

I don’t know why your indentions are not working, but don’t manually fix it.  Let me look.

Rob

I tried to manually space it below.   I appreciate your trying to help.

local movePath = {}

    movePath[1] = { x=680, y=253, time=8000 }

    movePath[2] = { x=680, y=840, time=8000 }

    movePath[3] = { x=105, y=840, time=8000 }

    movePath[4] = { x=105, y=253, time=8000 }

   movePath[5] = { x=384, y=253, time=8000 }

local function distBetween( x1, y1, x2, y2 )

     local xFactor = x2 - x1

     local yFactor = y2 - y1

     local dist = math.sqrt((xFactor*xFactor) + (yFactor*yFactor))

    return dist

end

local function setPath( object, path, params )

     local delta = params.useDelta or nil 

     local deltaX = 0

     local deltaY = 0

     local constant = params.constantTime or nil

     local ease = params.easingMethod or easing.linear

     local tag = params.tag or nil

     local delay = params.delay or 0

     local speedFactor = 1

     if ( delta and delta == false ) then

       --if delta = true, the object’s path will be relative to its starting position

        --if delta = false or ommitted, the path points will be explicit screen positions

          deltaX = object.x

         deltaY = object.y

     end

     if ( constant ) then

        --this sets a constant rate of movement

         local dist = distBetween( object.x, object.y, deltaX+path[1].x, deltaY+path[1].y )

         speedFactor = constant/dist

     end

     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

              function moveIt()

              moveHole =  transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease, onComplete=moveIt})

              delay = delay + segmentTime

               end

           end

end

setPath(pocket1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag=“moveObject” } )

setPath(pocketSensor1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag=“moveObject” } )

Just as an FYI, always use the and tags, even when you’re hand indenting your code.

Anyway, I don’t see an easy way to solve this.  Since you’re setting 8 total seconds, perhaps it’s best to use a timer:

local pocket1Timer = timer.performWithDelay(8000, function()     setPath(pocket1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } )     setPath(pocketSensor1, movePath, { useDelta=true, constantTime=8000, easingMethod=easing.inOutQuad, delay=200, tag="moveObject" } ) end, 0)