Confused with performwithdelay

I am trying to move the boxes one by one with a delay but I cannot figure it out how to make performwithdelay works. The counter I put says the function performs all at once. Any help on this?

local w,h = display.contentWidth, display.contentHeight local enemyNum = 4 --1-10 local enemy = {} for i = 1, enemyNum do enemy[i] = display.newRect( w+50, 50, 25, 25 ) end local function moveIt ( number ) --move to middle transition.to( enemy[number], { time=1500, x=w/2, y=50 } ) --move down transition.to( enemy[number], { time=1500, delay=1600, y=h/2 } ) -- move right transition.to( enemy[number], { time=1500, delay=3200, x=w+50 } ) print("This is loop no "..number) end for u = 1, enemyNum do local slowTime = 2000\*u timer.performWithDelay( slowTime, moveIt(u) ) end

Hi @morph180,

In your syntax, you’re specifying the listener parameter as a function (“moveIt()”), but I think you’re meaning to just state it as “moveIt” without the parentheses:

[lua]

timer.performWithDelay( slowTime, moveIt )

[/lua]

If you need to pass the value “u” along with the timer, there are two methods shown in the examples in the docs:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

Hope this helps,

Brent

The easiest approach might be similar to the “Passing Parameters (1)” section of Brent’s link … use an anonymous function:

for u = 1, enemyNum do local slowTime = 2000\*u timer.performWithDelay( slowTime, function() moveIt( u ) end ) end

i.e. the timer doesn’t directly trigger your function, but triggers the anonymous function which triggers your function (with extra arguments).

The fix works. Thanks everyone

Hi @morph180,

In your syntax, you’re specifying the listener parameter as a function (“moveIt()”), but I think you’re meaning to just state it as “moveIt” without the parentheses:

[lua]

timer.performWithDelay( slowTime, moveIt )

[/lua]

If you need to pass the value “u” along with the timer, there are two methods shown in the examples in the docs:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

Hope this helps,

Brent

The easiest approach might be similar to the “Passing Parameters (1)” section of Brent’s link … use an anonymous function:

for u = 1, enemyNum do local slowTime = 2000\*u timer.performWithDelay( slowTime, function() moveIt( u ) end ) end

i.e. the timer doesn’t directly trigger your function, but triggers the anonymous function which triggers your function (with extra arguments).

The fix works. Thanks everyone