problem with trasition.to continuous

Hello. I have a problem with this code. I want do a continuous movement but I can´t get it.

This is the code. Thank you.

function mov2 (self, position1, position2)

    local pos1 = position1

    local pos2 = position2

    local nom = self

    transition.to (nom, {time =2000, x= pos2, onComplete = mov1(nom, pos1, pos2) })

end

function mov1 (self, position1, position2)

    local pos1 = position1

    local pos2 = position2

    local nom = self

    transition.to (nom, {time =2000, x= pos1 , onComplete = mov2(nom, pos1, pos2)})  

end

rec1 = display.newRect (0,0,50,50)

rec2 = display.newRect (0,200,50,50)

mov1 (rec1, 300, 10)

mov1 (rec2, 300, 50)

Make your functions, anonymous functions assigned to upvalues.

var mov1, mov2 = function(self, position1, position2)…

mov1 = function(self, position1, position2)…

Hello jack69. I appreciate your answer but I don’t understand. Can you explain for me. Thank you

Don’t use self as an argument or variable name. It’s a keyword. Use anonymous functions and predeclared variables for this sort of thing.

I believe this should work:

var mov1, mov2 -- predeclared mov2 = function(something, position1, position2) -- anonymous function assignment! transition.to (nom, {time =2000, x= position2, onComplete = mov1(something, position1, position2) }) -- see you can reference mov1 now, even though it's nil at this point because we predclared it end -- Now we define it properly, before all this code executes mov1 = function(something, position1, position2) transition.to (nom, {time =2000, x= position1, onComplete = mov2(something, position1, position2)}) end var rec1 = display.newRect (0,0,50,50) var rec2 = display.newRect (0,200,50,50) -- call the function to start them both off mov1 (rec1, 300, 10) mov1 (rec2, 300, 50)

Thank you for your explanations, but I text the code but I did not get to work.

local mov1, mov2

mov2= function (something, position1, position2)

     transition.to (something, {time =2000, x= position2, onComplete = mov1(something, position1, position2)})

end

mov1= function (something, position1, position2)

    transition.to (something, {time =2000, x= position1, onComplete = mov2(something, position1, position2)})  

end

local  rec1 = display.newRect (0,0,50,50)

local  rec2 = display.newRect (0,200,50,50)

mov1 (rec1, 300, 10)

–mov1 (rec2, 300, 50)

There are many ways to do animation. This is one way. This code is tested and should run by pasting into main.lua

local object = {} function object:mov2(shape,position1,position2) transition.to (shape, {time =2000, x= position2, onComplete = function() object:mov1(shape, position1, position2) end}) end function object:mov1(shape, position1, position2) transition.to (shape, {time =2000, x= position1, onComplete = function() object:mov2(shape, position1, position2) end}) end local rec1 = display.newRect (0,0,50,50) local rec2 = display.newRect (0,200,50,50) object:mov1 (rec1, 300, 10) object:mov1 (rec2, 300, 50)

Thank you very much for your answer.

works perfectly

Make your functions, anonymous functions assigned to upvalues.

var mov1, mov2 = function(self, position1, position2)…

mov1 = function(self, position1, position2)…

Hello jack69. I appreciate your answer but I don’t understand. Can you explain for me. Thank you

Don’t use self as an argument or variable name. It’s a keyword. Use anonymous functions and predeclared variables for this sort of thing.

I believe this should work:

var mov1, mov2 -- predeclared mov2 = function(something, position1, position2) -- anonymous function assignment! transition.to (nom, {time =2000, x= position2, onComplete = mov1(something, position1, position2) }) -- see you can reference mov1 now, even though it's nil at this point because we predclared it end -- Now we define it properly, before all this code executes mov1 = function(something, position1, position2) transition.to (nom, {time =2000, x= position1, onComplete = mov2(something, position1, position2)}) end var rec1 = display.newRect (0,0,50,50) var rec2 = display.newRect (0,200,50,50) -- call the function to start them both off mov1 (rec1, 300, 10) mov1 (rec2, 300, 50)

Thank you for your explanations, but I text the code but I did not get to work.

local mov1, mov2

mov2= function (something, position1, position2)

     transition.to (something, {time =2000, x= position2, onComplete = mov1(something, position1, position2)})

end

mov1= function (something, position1, position2)

    transition.to (something, {time =2000, x= position1, onComplete = mov2(something, position1, position2)})  

end

local  rec1 = display.newRect (0,0,50,50)

local  rec2 = display.newRect (0,200,50,50)

mov1 (rec1, 300, 10)

–mov1 (rec2, 300, 50)

There are many ways to do animation. This is one way. This code is tested and should run by pasting into main.lua

local object = {} function object:mov2(shape,position1,position2) transition.to (shape, {time =2000, x= position2, onComplete = function() object:mov1(shape, position1, position2) end}) end function object:mov1(shape, position1, position2) transition.to (shape, {time =2000, x= position1, onComplete = function() object:mov2(shape, position1, position2) end}) end local rec1 = display.newRect (0,0,50,50) local rec2 = display.newRect (0,200,50,50) object:mov1 (rec1, 300, 10) object:mov1 (rec2, 300, 50)

Thank you very much for your answer.

works perfectly