Can I use arrays or something for object names?

Hello,

I am having to write an app where I am moving images around. And I have the coordinates of where I move them. What I want to do is pass the variable name to a function and have it move it for me. How can I do this? I really need to move two images at once so if I could pass the object names to a function and let the function move them with a timer that would be great!

Thanks,

Warren

Hi Warren,

Here is a simple example of one possible approach:

[lua]local imgs = {
  img1 = display.newImage( “img1.png” ),
  img2 = display.newImage( “img2.png” )
}

imgs.img1.x = display.contentCenterX
imgs.img1.y = display.contentCenterX

imgs.img2.x = display.contentCenterX
imgs.img2.y = display.contentCenterX

local function moveImgs( imgTable1, imgTable2 )
  transition.to( imgTable1.src, { x = imgTable1.x, y = imgTable1.y } )
  transition.to( imgTable2.src, { x = imgTable2.x, y = imgTable2.y } )
end

local imgTable1 = { src = imgs.img1, x = 20, y = 20 }
local imgTable2 = { src = imgs.img2, x = 200, y = 200 }

moveImgs( imgTable1, imgTable2 )[/lua]

Hi Warren,

Here is a simple example of one possible approach:

[lua]local imgs = {
  img1 = display.newImage( “img1.png” ),
  img2 = display.newImage( “img2.png” )
}

imgs.img1.x = display.contentCenterX
imgs.img1.y = display.contentCenterX

imgs.img2.x = display.contentCenterX
imgs.img2.y = display.contentCenterX

local function moveImgs( imgTable1, imgTable2 )
  transition.to( imgTable1.src, { x = imgTable1.x, y = imgTable1.y } )
  transition.to( imgTable2.src, { x = imgTable2.x, y = imgTable2.y } )
end

local imgTable1 = { src = imgs.img1, x = 20, y = 20 }
local imgTable2 = { src = imgs.img2, x = 200, y = 200 }

moveImgs( imgTable1, imgTable2 )[/lua]