Infinite-multiple transitions

I’m trying to create a reusable infinite-multiple transitions function that can be applied to any number of objects.

-- The transition function trans = function ( obj )     function forward(  )         local forward = transition.to( obj, {time=2000, x=obj.x-obj.width, onComplete=backward} )     end     function backward(  )         local back = transition.to( obj, {time=2000, x=obj.x+obj.width, onComplete=forward} )     end     forward( ) end

Create Object:

createRect = function ( x, y )     local rect = display.newRect( x, y, 100, 100 )     trans(rect) end

Spawn objects:

createRect(400, 200) -- transition happens once createRect(400, 400) -- transition loops as expected

createRect(400, 200) -- transition happens once createRect(400, 400) -- transition happens once createRect(400, 600) -- transition loops as expected

The problem is that the transition function works well if only one object was created, if there are more than one object, then the transition will execute once on all of them, but will only loop on the last object that was created. 
I hope I was able to clearly explain the problem. 

The main issue here is with scope, which is why you are encountering this issue. The below tutorial will walk you through the more important points:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Regarding your issue, try this:

 local function example1ObjConst(obj) local function example2ObjConst() transition.to(obj, {time=500, x=obj.x-obj.width, onComplete=example1ObjConst}) end transition.to(obj, {time=500, x=obj.x+obj.width, onComplete=example2ObjConst}) end local createRect = function ( x, y ) local rect = display.newRect( x, y, 100, 100 ) example1ObjConst(rect) end

Same thing as Alex but in the original format

trans = function (obj) local forward,backward function forward() transition.to(obj, {time=2000, x=obj.x-obj.width, onComplete=backward}) end function backward() transition.to(obj, {time=2000, x=obj.x+obj.width, onComplete=forward}) end forward() end local createRect = function (x, y) local rect = display.newRect(x, y, 100, 100) trans(rect) end createRect(200, 200) createRect(200, 400)

Yet another approach, may be a bit lighter-weight as the functions are only defined once.

local forward local backward forward = function( self ) self.onComplete = backward transition.to(self, {time=2000, x=obj.x-obj.width, onComplete = self }) end backward = function( self ) self.onComplete = forward transition.to(self, {time=2000, x=obj.x+obj.width, onComplete = self}) end local function createRect( x, y, back ) local rect = display.newRect( x, y, 100, 100) if( back ) then backward( rect ) else forward( rect ) end end createRect(200, 200) -- move forward first (by default) createRect(400, 200, true) -- move backward first

I don’t understand, why does it work in the case of a single created rect, or on the final created rect?

You’re creating a bunch of global functions which are replacing the prior definitions and corrupting subsequent calls.

You need to be much more careful about globals.  In fact don’t make them at all without full knowledge of the side-effects and purpose.

98% of your functions and variables will typically be locals or assigned as fields to objects/tables.  

Your code is pretty much 100% globals.

The main issue here is with scope, which is why you are encountering this issue. The below tutorial will walk you through the more important points:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Regarding your issue, try this:

 local function example1ObjConst(obj) local function example2ObjConst() transition.to(obj, {time=500, x=obj.x-obj.width, onComplete=example1ObjConst}) end transition.to(obj, {time=500, x=obj.x+obj.width, onComplete=example2ObjConst}) end local createRect = function ( x, y ) local rect = display.newRect( x, y, 100, 100 ) example1ObjConst(rect) end

Same thing as Alex but in the original format

trans = function (obj) local forward,backward function forward() transition.to(obj, {time=2000, x=obj.x-obj.width, onComplete=backward}) end function backward() transition.to(obj, {time=2000, x=obj.x+obj.width, onComplete=forward}) end forward() end local createRect = function (x, y) local rect = display.newRect(x, y, 100, 100) trans(rect) end createRect(200, 200) createRect(200, 400)

Yet another approach, may be a bit lighter-weight as the functions are only defined once.

local forward local backward forward = function( self ) self.onComplete = backward transition.to(self, {time=2000, x=obj.x-obj.width, onComplete = self }) end backward = function( self ) self.onComplete = forward transition.to(self, {time=2000, x=obj.x+obj.width, onComplete = self}) end local function createRect( x, y, back ) local rect = display.newRect( x, y, 100, 100) if( back ) then backward( rect ) else forward( rect ) end end createRect(200, 200) -- move forward first (by default) createRect(400, 200, true) -- move backward first

I don’t understand, why does it work in the case of a single created rect, or on the final created rect?

You’re creating a bunch of global functions which are replacing the prior definitions and corrupting subsequent calls.

You need to be much more careful about globals.  In fact don’t make them at all without full knowledge of the side-effects and purpose.

98% of your functions and variables will typically be locals or assigned as fields to objects/tables.  

Your code is pretty much 100% globals.