Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

For loop question?
Started by cindy.h1986 Aug 18 2013 04:10 PM

- - - - -
2 replies to this topic
for loop grid array table spawn
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 115 posts
  • Corona SDK

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?

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    endend

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 1,051 posts
  • Corona SDK

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


local myRects = {{x=0, y=0}, {x=40, y=0} etc...}


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.


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  

Not tested, but gets you started hopefully.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 11 posts
  • Corona SDK

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