How to reorder object index numbers on a table

Ok, so here’s my problem. I have a screen filled with cubes. If I want to switch the position of one cube with another I simply swipe, and I am able to index both cubes to do this. No problem there, BUT if I try doing it again or with a different cube 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 numbers every time I do a swipe? 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

Hi @alex_r_101,

As a quick general question, when you swap cubes around, do you want the cubes to be represented by their new location? For example, if you start with {1,2,3}, and you swap 1 and 2, you’ll get {2,1,3}. But, in that case, should cube 2 actually become cube 1? And cube 2 become 1? Meaning, do they swap, and you need to swap the positions as well?

Best regards,

Brent

Yes, Brent, that is exactly what I’m looking for, and what’s causing all the problems with the swipe function. I don’t how to change the array positions. The cubes swap, but not their place in the table, so that creates malfunction when I do more than one swipe.

Anybody? I’m not even sure that this is possible in Lua…  :mellow:

Hi @alex_r_101,

This should certainly be possible in Lua with the proper setup. Instead of just trying to shift/swap items around in an indexed table, how about using a multi-dimensional array and then swapping items between places that are already established as placeholders in the array?

Brent

Hi @alex_r_101,

As a quick general question, when you swap cubes around, do you want the cubes to be represented by their new location? For example, if you start with {1,2,3}, and you swap 1 and 2, you’ll get {2,1,3}. But, in that case, should cube 2 actually become cube 1? And cube 2 become 1? Meaning, do they swap, and you need to swap the positions as well?

Best regards,

Brent

Yes, Brent, that is exactly what I’m looking for, and what’s causing all the problems with the swipe function. I don’t how to change the array positions. The cubes swap, but not their place in the table, so that creates malfunction when I do more than one swipe.

Anybody? I’m not even sure that this is possible in Lua…  :mellow:

Hi @alex_r_101,

This should certainly be possible in Lua with the proper setup. Instead of just trying to shift/swap items around in an indexed table, how about using a multi-dimensional array and then swapping items between places that are already established as placeholders in the array?

Brent