images in a grid with for?

hello guys, i want to make with a for loop something like a grid, means i have 8 images, with for i can display 4 of them in a row along the x, but now i want to display the next 4 in a row beneath.

how is it possible?

my code:

-- display the four balls on the screen and tweak their parameters local function setUpBalls() for i=1, 5 do local idx = colorBalls[i] mainBalls[idx] = display.newImage("images/" .. colorBalls[i] .. ".png") mainBalls[idx].height = 110 mainBalls[idx].width = 110 mainBalls[idx].x = 138 \* i - 24 mainBalls[idx].y = 20 \* i mainBalls[idx].alpha = 0.7 mainBalls[idx].color = colorBalls[i] mainBalls[idx].sound = audio.loadSound("audio/" .. colorBalls[i] .. ".aiff") mainBalls[idx]:addEventListener ( "touch", ballTouched ) end end

To do a 2-d array like that, you need a nested loop

for i = 1, numberOfRows do for j = 1, numberOfColumns do -- add cell {i, j} end end

Hmm is it really so easy?
What’s with the mainBalls function? There are all the images in a table, do I need a second table to avoid repeating the balls?

You don’t need a second table, just use an extra “count” variable as you go along:

local count = 1 for i = 1, numberOfRows do for j = 1, numberOfColumns do local idx = colorBalls[count] mainBalls[idx] = display.newImage("images/" .. colorBalls[count] .. ".png") mainBalls[idx].height = 110 mainBalls[idx].width = 110 mainBalls[idx].x = 138 \* i - 24 mainBalls[idx].y = 20 \* j mainBalls[idx].alpha = 0.7 mainBalls[idx].color = colorBalls[count] mainBalls[idx].sound = audio.loadSound("audio/" .. colorBalls[count] .. ".aiff") mainBalls[idx]:addEventListener ( "touch", ballTouched ) count = count + 1 end end

Something like that.

it works thank you a lot

By the way, you probably shouldn’t call audio.loadSound() for every cell in the loop. That loads the entire sound file into memory i * j times. You should only load the sound file once. display.newImage() doesn’t suffer from this as it recognizes the image has been loaded already, but I don’t think that is the case with audio.loadSound()

To do a 2-d array like that, you need a nested loop

for i = 1, numberOfRows do for j = 1, numberOfColumns do -- add cell {i, j} end end

Hmm is it really so easy?
What’s with the mainBalls function? There are all the images in a table, do I need a second table to avoid repeating the balls?

You don’t need a second table, just use an extra “count” variable as you go along:

local count = 1 for i = 1, numberOfRows do for j = 1, numberOfColumns do local idx = colorBalls[count] mainBalls[idx] = display.newImage("images/" .. colorBalls[count] .. ".png") mainBalls[idx].height = 110 mainBalls[idx].width = 110 mainBalls[idx].x = 138 \* i - 24 mainBalls[idx].y = 20 \* j mainBalls[idx].alpha = 0.7 mainBalls[idx].color = colorBalls[count] mainBalls[idx].sound = audio.loadSound("audio/" .. colorBalls[count] .. ".aiff") mainBalls[idx]:addEventListener ( "touch", ballTouched ) count = count + 1 end end

Something like that.

it works thank you a lot

By the way, you probably shouldn’t call audio.loadSound() for every cell in the loop. That loads the entire sound file into memory i * j times. You should only load the sound file once. display.newImage() doesn’t suffer from this as it recognizes the image has been loaded already, but I don’t think that is the case with audio.loadSound()