Game logic Loop?

I am trying to become a better coder, which for me means to consolidate my code and stop “hacking it”.

I have 9 images and im trying to use a loop.
This is just an example of my images

 local myObject = display.newImageRect( "wallpapers/1.png",320,480 )  
 myObject.x = \_W/5 -2  
 myObject.y = 107  
 myObject.xScale = .25  
 myObject.yScale = .25  
  
 local myObject2 = display.newImageRect( "wallpapers/2.png",320,480 )  
 myObject2.x = \_W/2  
 myObject2.y = 107  
 myObject2.xScale = .25  
 myObject2.yScale = .25  
  
 local myObject3 = display.newImageRect( "wallpapers/3.png",320,480 )  
 myObject3.x = \_W - \_W/5 + 2  
 myObject3.y = 107  
 myObject3.xScale = .25  
 myObject3.yScale = .25  
  
 local myObject4 = display.newImageRect( "wallpapers/4.png",320,480 )  
 myObject4.x = \_W/5 - 2  
 myObject4.y = 104+130  
 myObject4.xScale = .25  
 myObject4.yScale = .25  
  
 local myObject5 = display.newImageRect( "wallpapers/5.png",320,480 )  
 myObject5.x = \_W/2  
 myObject5.y = 104+130  
 myObject5.xScale = .25  
 myObject5.yScale = .25  
  
 local myObject6 = display.newImageRect( "wallpapers/6.png",320,480 )  
 myObject6.x = \_W - \_W/5 + 2  
 myObject6.y = 104+130  
 myObject6.xScale = .25  
 myObject6.yScale = .25  
  
  
 local myObject7 = display.newImageRect( "wallpapers/7.png" ,320,480)  
 myObject7.x = \_W/5 - 2  
 myObject7.y =104+130+130  
 myObject7.xScale = .25  
 myObject7.yScale = .25  
  
 local myObject8 = display.newImageRect( "wallpapers/8.png" ,320,480)  
 myObject8.x = \_W/2  
 myObject8.y = 104+130+130  
 myObject8.xScale = .25  
 myObject8.yScale = .25  
  
 local myObject9 = display.newImageRect( "wallpapers/9.png" ,320,480)  
 myObject9.x = \_W - \_W/5 + 2  
 myObject9.y = 104+130+130  
 myObject9.xScale = .25  
 myObject9.yScale = .25  

Now i am trying to make that with a loop or something…
Any help?
Im not asking for anyone to make the code for me i just want to be pointed in the right direction thanks.
[import]uid: 24708 topic_id: 27661 reply_id: 327661[/import]

local myObject = {}  
local xObject = {} -- fill in values of x here for objects 1-9  
local yObject = {} -- fill in values of y here for objects 1-9  
  
for i = 1, 9 do  
 myObject[i] = display.newImageRect( "wallpapers/" .. i .. ".png",320,480 )  
 myObject[i].x = xObject[i]  
 myObject[i].y = yObject[i]  
 myObject[i].xScale = .25  
 myObject[i].yScale = .25  
end  
  

something like this? havent studied how you place the objects so might even put that in somehow. I just put up an array that you fill out with the x and y values. [import]uid: 17969 topic_id: 27661 reply_id: 112225[/import]

Wow! Thanks guys!

I just have one more question that relates to this topic, if you can help me out that would be awesome!
If i put a listener in the loop like this…

function highlight(event)  
 if (event.phase == "ended") then  
  
 print(event.target)  
 end  
end  
for i=1, 9 do  
 rect[i] = display.newImageRect( "wallpapers/" .. i .. ".png",320,480 )  
  
 rect[i].x = (i%3-1)\*spacingX + (\_W/5 -2)  
 rect[i].y = math.floor(i/3)\*spacingY + 107  
 rect[i].xScale = .25  
 rect[i].yScale = .25  
 rect[i]:addEventListener("touch", highlight)  
end  
---this might not be a working code, i just made it up as an example  

How would i be able to get the actual name of the object when it prints to the terminal instead of it giving me a random table value?
[import]uid: 24708 topic_id: 27661 reply_id: 112300[/import]

Add a custom property to the object during creation:

  
rect[i].id = "wallpaper"..i  
  

Then print the id with:

print(event.target.id)  

[import]uid: 33275 topic_id: 27661 reply_id: 112311[/import]

[lua]local spacingX = _W/2 - _W/5 + 2
local spacingY = 130

for i=1, 9 do
– assuming your initial x and y is (_W/5 -2) and 107

– the first column is always of x-coord (_W/5 -2), so you obtain the modulo of 3 subtract 1 to obtain the
– number of spacings the object is from the first column
– for eg. if you’re the 5th object, you’d be in the second column so 5%3-1 gives you 1 spacing away
rect[i].x = (i%3-1)*spacingX + (_W/5 -2)

– since 1-3, 4-6, 7-9 all have same sets of y-coord, you divide the index by 3 and obtain the floor
– that’d be the spacing the object is from the first row.
– for eg. index 2 is in first row. math.floor(2/3) gives you 0, which is 0 spaces away from the first row
– since 3/3 gives you a full number 1, you’d have to subtract the value by one in the columns multiple of 3
local mult = math.floor(i/3)
if mult == i/3 then mult = mult - 1 end
rect[i].y = mult*spacingY + 107
end[/lua]

[Edit] Fixed a logic error in the y-coord loop [import]uid: 108204 topic_id: 27661 reply_id: 112226[/import]