iterating through a loop with a delay

Say I have a loop that creates a number of objects on screen in a grid pattern, how would I go about adding a delay to that loop so that each object appears a short time (say 500ms) after the previous one rather than all at once? 

sy=50 for r=1, 10 do sx=112 for c=1, 14 do foo=display.newCircle(sx,sy,30) sx=sx+40 end sy=sy+40 end  

Bearing in mind that while this is being done other stuff still has to be happening as well, so this can’t tie up the system (if that makes sense).

The loops is going to run as fast as it can. Corona is an event driven system so busy-wait’s block the interface. You would want to use a timer (or series of timers)

sy=50 count = 0 for r=1, 10 do sx=112 for c=1, 14 do count = count + 1 timer.performWithDelay(500 \* count, function() foo=display.newCircle(sx,sy,30) end) sx=sx+40 end sy=sy+40 end

Or something like that (Untested)

Rob

Method #1 - Co-routines
One way to achieve a delay is via a co-routine.  SSK2 comes with StarCrunch’s wait utilities :

-- Method 1 (Co-routines) local foos = {} local waitTime = 500 local function newFoo( x, y ) local foo = display.newCircle( x, y, 30 ) foos[#foos+1] = foo print( "Foo# " .. #foos .. " created at \<" .. foo.x .. " ," .. foo.y .. "\> @ time: " .. system.getTimer() ) end local function waitLoop() local sy = 50 local sx = 112 for r = 1, 10 do sx = 112 for c = 1, 14 do newFoo( sx, sy, foo ) sx = sx + 40 ssk.wait.ms( 500 ) end sy = sy + 40 end end ssk.wait.run( waitLoop )

 
Method #2 - performWithDelay

-- Method 2 Timers local foos = {} local delayTime = 500 local function createLoop() local count = 0 local sy = 50 local sx = 112 for r = 1, 10 do sx = 112 for c = 1, 14 do local x,y = sx,sy -- Important, or all objects will be created at last sx,sy timer.performWithDelay( count \* delayTime, function() local foo = display.newCircle( x, y, 30 ) foos[#foos+1] = foo print( "Foo# " .. #foos .. " created at \<" .. foo.x .. " ," .. foo.y .. "\> @ time: " .. system.getTimer() ) end ) count = count + 1 sx = sx + 40 end sy = sy + 40 end end nextFrame( createLoop )

Looks like Rob and I were posting at the same time.  

If you use Rob’s variant, make this change:

sy=50 count = 0 for r=1, 10 do sx=112 for c=1, 14 do count = count + 1 local x,y = sx, sy -- CHANGED timer.performWithDelay(500 \* count, function() foo=display.newCircle( x, y,30) -- CHANGED end) sx=sx+40 end sy=sy+40 end

Brilliant.  Thanks again guys :slight_smile:

The loops is going to run as fast as it can. Corona is an event driven system so busy-wait’s block the interface. You would want to use a timer (or series of timers)

sy=50 count = 0 for r=1, 10 do sx=112 for c=1, 14 do count = count + 1 timer.performWithDelay(500 \* count, function() foo=display.newCircle(sx,sy,30) end) sx=sx+40 end sy=sy+40 end

Or something like that (Untested)

Rob

Method #1 - Co-routines
One way to achieve a delay is via a co-routine.  SSK2 comes with StarCrunch’s wait utilities :

-- Method 1 (Co-routines) local foos = {} local waitTime = 500 local function newFoo( x, y ) local foo = display.newCircle( x, y, 30 ) foos[#foos+1] = foo print( "Foo# " .. #foos .. " created at \<" .. foo.x .. " ," .. foo.y .. "\> @ time: " .. system.getTimer() ) end local function waitLoop() local sy = 50 local sx = 112 for r = 1, 10 do sx = 112 for c = 1, 14 do newFoo( sx, sy, foo ) sx = sx + 40 ssk.wait.ms( 500 ) end sy = sy + 40 end end ssk.wait.run( waitLoop )

 
Method #2 - performWithDelay

-- Method 2 Timers local foos = {} local delayTime = 500 local function createLoop() local count = 0 local sy = 50 local sx = 112 for r = 1, 10 do sx = 112 for c = 1, 14 do local x,y = sx,sy -- Important, or all objects will be created at last sx,sy timer.performWithDelay( count \* delayTime, function() local foo = display.newCircle( x, y, 30 ) foos[#foos+1] = foo print( "Foo# " .. #foos .. " created at \<" .. foo.x .. " ," .. foo.y .. "\> @ time: " .. system.getTimer() ) end ) count = count + 1 sx = sx + 40 end sy = sy + 40 end end nextFrame( createLoop )

Looks like Rob and I were posting at the same time.  

If you use Rob’s variant, make this change:

sy=50 count = 0 for r=1, 10 do sx=112 for c=1, 14 do count = count + 1 local x,y = sx, sy -- CHANGED timer.performWithDelay(500 \* count, function() foo=display.newCircle( x, y,30) -- CHANGED end) sx=sx+40 end sy=sy+40 end

Brilliant.  Thanks again guys :slight_smile: