Switch the array position # of an array object

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

Swap their positions in the table as well as their physical positions.

[lua]

myTable[i], myTable[nextCubeLeft] = myTable[nextCubeLeft], myTable[i]

[/lua]

Thank you that was it! I had originally tried this, but I gave up easily when I encountered a nil value error message because I was sure I was doing something that just wasn’t possible.

Anyways I used your concept except I declared the values before.

notice tablePosition1 and 2, and then I used the delay because otherwise the transition would also give a nil value error message.

nextCubeLeft = i - 1 

tablePosition1 = myTable[i]

tablePosition2 = myTable[nextCubeLeft]

position1 = myTable[i].x

position2 = myTable[nextCubeLeft].x

transition.to( myTable[i], { time=50, x= position2}) --RIGHTCUBE

        transition.to( myTable[nextCubeLeft], { time=50, x= position1 } ) --LEFTCUBE

function delay()

myTable[i], myTable[nextCubeLeft] = tablePosition2, tablePosition1

end

timer.performWithDelay(100, delay)

Swap their positions in the table as well as their physical positions.

[lua]

myTable[i], myTable[nextCubeLeft] = myTable[nextCubeLeft], myTable[i]

[/lua]

Thank you that was it! I had originally tried this, but I gave up easily when I encountered a nil value error message because I was sure I was doing something that just wasn’t possible.

Anyways I used your concept except I declared the values before.

notice tablePosition1 and 2, and then I used the delay because otherwise the transition would also give a nil value error message.

nextCubeLeft = i - 1 

tablePosition1 = myTable[i]

tablePosition2 = myTable[nextCubeLeft]

position1 = myTable[i].x

position2 = myTable[nextCubeLeft].x

transition.to( myTable[i], { time=50, x= position2}) --RIGHTCUBE

        transition.to( myTable[nextCubeLeft], { time=50, x= position1 } ) --LEFTCUBE

function delay()

myTable[i], myTable[nextCubeLeft] = tablePosition2, tablePosition1

end

timer.performWithDelay(100, delay)