0

I have this function

local function cambiodesp(old,new)
   block[new]= block[old]
end

and i have this:

random=4 
local image = display.newImage(images[random], 0, 0)
image.value = random
image.x = 161
image.y = 451
block[0] = image

random=1
image.value = random 
local image = display.newImage(images[random], 0, 0)
image.x = 161
image.y = 515
block[1] = image

i want to change the block.value of the images, the reference of the images in block, but i dont want to change the coordinates. I'm calling the function:

cambiodes(0,1)

but the coordinates are changing and i don`t know what to do.

I hope you can give me an answer, advice or whatever you think.

Thank you very much

Rodrigo López
  • 227
  • 4
  • 17

2 Answers2

0

You are copying the entire table (reference); if you want to keep x and y coordinates, just save them and re-assign (assuming everything else happens as you want it):

local function cambiodesp(old,new)
   local x, y = block[new].x, block[new].y
   block[new]= block[old]
   block[new].x, block[new].y = x,y
end
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thank you for your answer, but this is not working. Because the coordinates of block[old] are changing. before the function were 451 and after the function were 515. And i try to to this: local x, y = block[new].x, block[new].y local x2, y2 = block[old].x, block[old].y block[new]= block[old] block[new].x, block[new].y = x,y block[old].x, block[old].y = x2,y2 (it doesn`t work, please please help me). Thank you very much – Rodrigo López Jun 20 '13 at 04:50
0
local function swap(old,new)
    block[old], block[new] = block[new], block[old] #swap references
    block[old].x, block[old].y, block[new].x, block[new].y = block[new].x, block[new].y, block[old].x, block[old].y #re-set coordinates
end
HennyH
  • 7,794
  • 2
  • 29
  • 39