Dragging, center and swapping places of tiles/objects?

Hey guys, 

I created a few rects that I made draggable, when I release them they transition to the same position and stack ontop of eachother. How do I make them line up next to each other after they have been “released” and then move so they all stay centered?

What I mean is, I touch a rect, drag it and release, first rect transition to  centerX, next rect ( when released) transitions to the right of the prev rect and they move left so they stay centered on screen.

If I want to change places of already touched rects then I can drag them around and swap positions, rects that have not yet been transitioned should not be “rearrangable”?

I’m trying to utilize all my reading so far and put it in practise…

thanks

local centerX = display.contentWidth \* 0.5 local tileArray = {} -- Just a rect to see where the tiles transition to, not of importance. local stripe = display.newRect(0, 170, display.contentWidth, 60) stripe:setFillColor(100, 1, 22, 255) function touchListener(event) local phase = event.phase local target = event.target if phase == "began" then print(" Touch Tile in Phase : ", phase) display.getCurrentStage():setFocus(target) target.isFocus = true target.markX = target.x target.markY = target.y elseif phase == "moved" then print(" Touch Tile in Phase : ", phase) target.x = event.x - event.xStart + target.markX target.y = event.y - event.yStart + target.markY elseif phase == "ended" or phase == "cancelled" then print(" Touch Tile in Phase : ", phase) display.getCurrentStage():setFocus(nil) target.isFocus = false transition.to(target, { x = centerX, y = 200, time = 250, delay = 0, alpha = 1.0, transition = easing.inExpo, onComplete = function() target:removeEventListener("touch", touchListener) end }) end return true end for i = 1, 5 do tileArray[#tileArray + 1] = display.newRect(0, 0, 50, 50) local tile = tileArray[#tileArray] tile.x = 50 + (i - 1) \* 55 tile.y = display.contentHeight - 100 tile:setFillColor(math.random(0, 255), math.random(0, 255), math.random(0, 255), 255) tile:addEventListener("touch", touchListener) end

I’ve made some progress though I know it’s not the right way and it’s not working like I’d hoped so If anyone can give me some advice what to do with my swapping/rearrange of the tiles.

thanks.

local tileArray = {} local tempArray = {} local colors = {"Green", "Red", "Orange", "Blue", "White"} -- tile id's local letters = {"G", "R", "O", "B", "W"} -- if the user is colorblind   -- Just a rect to see where the tiles transition to, not of importance. local stripe = display.newRect(0, 170, display.contentWidth, 60)       stripe:setFillColor(100, 1, 22, 255)   function touchListener(event)     local phase = event.phase     local target = event.target              if phase == "began" then                  print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(target)           target.markX = target.x         target.markY = target.y               elseif phase == "moved" then           print(" Touch Tile in Phase : ", phase)         target.x = event.x - event.xStart + target.markX         target.y = event.y - event.yStart + target.markY       elseif phase == "ended" or phase == "cancelled" then           print(" Touch Tile in Phase : ", phase, target.color)         display.getCurrentStage():setFocus(nil)             transition.to(target, {                x = 160,                y = 200,                time = 250,                delay = 0,                alpha = 1.0,                transition = easing.inExpo,                onComplete = function()                    tempArray[#tempArray + 1] = target                    target:removeEventListener("touch", touchListener)              end })                 for i = 1, #tempArray do                 local tempTile = tempArray[#tempArray]                       tempTile.x = 50 + (i - 1) \* 55                       tempTile:addEventListener("touch", reArrangeListener)             end     end          return true end       for i = 1, 5 do     tileArray[#tileArray + 1] = display.newRect(0, 0, 50, 50)       local tile = tileArray[#tileArray]           tile.x = 50 + (i - 1) \* 55           tile.y = display.contentHeight - 100           tile.color = colors[i]           tile.letter = letters[i]           tile:setFillColor(math.random(0, 255), math.random(0, 255), math.random(0, 255), 255)             tile:addEventListener("touch", touchListener) end     function reArrangeListener(event)     local phase = event.phase     local target = event.target           target:toFront()     if phase == "began" then                  print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(target)           target.markX = target.x             transition.to(target, { time = 50, delay = 0, xScale = 1.25, yScale = 1.25, transition = easing.inExpo, onComplete = function() end})          elseif phase == "moved" then           print(" Touch Tile in Phase : ", phase)         target.x = event.x - event.xStart + target.markX        elseif phase == "ended" or phase == "cancelled" then           print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(nil)         transition.to(target, { time = 50, delay = 0, xScale = 1.0, yScale = 1.0, transition = easing.inExpo, onComplete = function() end})     end          return true end  

I’ve made some progress though I know it’s not the right way and it’s not working like I’d hoped so If anyone can give me some advice what to do with my swapping/rearrange of the tiles.

thanks.

local tileArray = {} local tempArray = {} local colors = {"Green", "Red", "Orange", "Blue", "White"} -- tile id's local letters = {"G", "R", "O", "B", "W"} -- if the user is colorblind   -- Just a rect to see where the tiles transition to, not of importance. local stripe = display.newRect(0, 170, display.contentWidth, 60)       stripe:setFillColor(100, 1, 22, 255)   function touchListener(event)     local phase = event.phase     local target = event.target              if phase == "began" then                  print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(target)           target.markX = target.x         target.markY = target.y               elseif phase == "moved" then           print(" Touch Tile in Phase : ", phase)         target.x = event.x - event.xStart + target.markX         target.y = event.y - event.yStart + target.markY       elseif phase == "ended" or phase == "cancelled" then           print(" Touch Tile in Phase : ", phase, target.color)         display.getCurrentStage():setFocus(nil)             transition.to(target, {                x = 160,                y = 200,                time = 250,                delay = 0,                alpha = 1.0,                transition = easing.inExpo,                onComplete = function()                    tempArray[#tempArray + 1] = target                    target:removeEventListener("touch", touchListener)              end })                 for i = 1, #tempArray do                 local tempTile = tempArray[#tempArray]                       tempTile.x = 50 + (i - 1) \* 55                       tempTile:addEventListener("touch", reArrangeListener)             end     end          return true end       for i = 1, 5 do     tileArray[#tileArray + 1] = display.newRect(0, 0, 50, 50)       local tile = tileArray[#tileArray]           tile.x = 50 + (i - 1) \* 55           tile.y = display.contentHeight - 100           tile.color = colors[i]           tile.letter = letters[i]           tile:setFillColor(math.random(0, 255), math.random(0, 255), math.random(0, 255), 255)             tile:addEventListener("touch", touchListener) end     function reArrangeListener(event)     local phase = event.phase     local target = event.target           target:toFront()     if phase == "began" then                  print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(target)           target.markX = target.x             transition.to(target, { time = 50, delay = 0, xScale = 1.25, yScale = 1.25, transition = easing.inExpo, onComplete = function() end})          elseif phase == "moved" then           print(" Touch Tile in Phase : ", phase)         target.x = event.x - event.xStart + target.markX        elseif phase == "ended" or phase == "cancelled" then           print(" Touch Tile in Phase : ", phase)         display.getCurrentStage():setFocus(nil)         transition.to(target, { time = 50, delay = 0, xScale = 1.0, yScale = 1.0, transition = easing.inExpo, onComplete = function() end})     end          return true end