Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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

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

cindy.h1986

[GLOBAL: userInfoPane.html]
cindy.h1986
  • 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]
#2

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • 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  
end

Not tested, but gets you started hopefully.



[TOPIC: post.html]
#3

Vinreal

[GLOBAL: userInfoPane.html]
Vinreal
  • Observer

  • 11 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]