[HELP] Exchange positions of two object

I need to make something like this:

I have got 2 squares - If I click on 1 square it change their image to selected… Then I click on second square and this squares exchange their positions… You can image it like Bejeweled blitz game… you choose one diamond and then click other and they exchange their positions…

So I do this…
When I touch first square I write its position to 2 variables:
Square1X
Square1Y

Now, when I clicked on second square I write its position to:
Square2X
Square2Y
Then I set position of first block position to Square2X,Square2Y and second block position to Square1X,Square1X…

transition.to( e.target, { time=300, x=Square1X, transition=easing.linear }) transition.to( e.target, { time=300, y=Square1Y, transition=easing.linear }) transition.to( square, { time=300, x=Square2X, transition=easing.linear }) transition.to( square, { time=300, y=Square2Y, transition=easing.linear })

then I reset this values…

Works great with 2 squares, but if I add 3rd square it stop working… Problem is probably this:

I’m transitioning “square” a “e.target.” (what is 1st square…), but if there is more squares this transitioning stop working…

How can I make this working with more than 2 “squares” ?
Thanks. [import]uid: 59968 topic_id: 11942 reply_id: 311942[/import]

maybe its not exactly what you need, but anyway it works, 3 squares going around changing positions
its not working properly enough, so need some work on balancing

[code]
local square1 = display.newRect(0,0,50,50)
square1.x = 150
square1.y = 150

local square2 = display.newRect(0,0,50,50)
square2:setFillColor(255,15,15)
square2.x = 150
square2.y = 350

local square3 = display.newRect(0,0,50,50)
square3:setFillColor(15,255,24)
square3.x = 350
square3.y = 250

local function transit(event)
transition.to( square1, { time=2000, x=square3.x, y=square3.y } )
transition.to( square2, { time=2000, x=square1.x, y=square1.y } )
transition.to( square3, { time=2000, x=square2.x, y=square2.y } )
end

square1:addEventListener(“touch”, transit)

[/code] [import]uid: 16142 topic_id: 11942 reply_id: 43542[/import]

If you are clicking three squares, when are they supposed to switch places? After the third touch?

I understand what you’re doing with 2 squares, but not sure what you mean by touching a 3rd square.

Can you clarify?

Jay [import]uid: 9440 topic_id: 11942 reply_id: 43570[/import]

I solved it myself. Thanks. [import]uid: 59968 topic_id: 11942 reply_id: 43608[/import]