I thought I’d post this here, because it’s more of a Lua language thing.
Ok, so here’s my problem. I have a screen filled with cubes. If I want to switch the ARRAY position of one cube with another I simply swipe, and I am able to index both cubes to do this. I am able to move their PHYSICAL positions. No problem there, BUT if I try doing it again or with a different cube physical positions get messed up. Why?
With the function that I have I index the event.target with table[i] and for the cube next to it I use table[cubeRight] (note: cubeRight = i + 1). Let me make an example
First order 1 , 2, 3
Swipe 1 with 2
New order is 2, 1, 3
Try to swipe 2 with 1 and instead of 1,2,3 I get 3,1,2 because the cubeRight = i + 1 calls the next number up.
I’ve thought about table.sort after the transition is completed, but 1) I don’t know if this is what I need, and 2) When I try to use it I can’t execute it right.
Is there a way that I can reset the array position numbers every time I do a swipe? I can switch their physical position, but not their array # position.
Here’s my code, I appreciate your help!
local function Go ( event )
if event.phase == “began” then
display.getCurrentStage():setFocus(event.target, event.id)
event.target.isFocus = true
beginX = event.x
beginY = event.y
elseif event.target.isFocus then
if event.phase == “ended” then
display.getCurrentStage():setFocus( nil)
self.isFocus = false
endX = event.x
endY = event.y
for i = #myTable, 1, -1 do
object = myTable[i]
--moveLeft
if object == event.target then
if beginX > endX - 5 then
nextCubeRight = i - 1
position1 = myTable[i].x
position2 = myTable[nextCubeRight].x
reOrderArray1 = myTable[i]
reOrderArray2 = myTable[nextCubeRight]
transition.to( myTable[i], { time=500, x= myTable[i].x - 128} )
transition.to( myTable[nextCubeRight], { time=500, x= position1 } )
end
end
--moveRight
if object == event.target then
if beginX < endX + 5 then
nextCubeLeft = i + 1
position1 = myTable[i].x
position2 = myTable[nextCubeLeft].x
transition.to( myTable[i], { time=500, x= position2} )
transition.to( myTable[nextCubeLeft], { time=500, x= position1 } )
end
end
end
end
end
end