For loop question?

This create a 4x4 grid of rects instantly, how do I make them appear randomly in its positions one after the other until all of them are on screen?

[lua]
local tiles = {}

function createBoard()
     for i = 1, 4 do
        for j = 1, 4 do
            tiles[i] = display.newRect(40, 40, 0, 0)
            tiles[i].x = 50*(i-1)
            tiles[i].y = 50*(j-1)

       end
    end
end

[/lua]

You could create a table with the x and y values for each rect.

[lua]local myRects = {{x=0, y=0}, {x=40, y=0} etc…}[/lua]

Use the loop you already have to make this table. Then shuffle the table (randomising it).

Then you would iterate through the randomised table to render your rects but adding a delay.

[lua]

local delay = 0

for i = 1, #myRects do

  timer.performWithDelay(delay, function() display.newRect(40, 40, myRects[i].x, myRects[i].y) end)

  delay = delay + 100  

end

[/lua]

Not tested, but gets you started hopefully.

You could create a table with the x and y values for each rect.

[lua]local myRects = {{x=0, y=0}, {x=40, y=0} etc…}[/lua]

Use the loop you already have to make this table. Then shuffle the table (randomising it).

Then you would iterate through the randomised table to render your rects but adding a delay.

[lua]

local delay = 0

for i = 1, #myRects do

  timer.performWithDelay(delay, function() display.newRect(40, 40, myRects[i].x, myRects[i].y) end)

  delay = delay + 100  

end

[/lua]

Not tested, but gets you started hopefully.

Thank you very much, jonjonsson. This works like a charm!

Thank you very much, jonjonsson. This works like a charm!