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.