issue passing variables to function with timer.performWithDelay

hello!

i’ve playing with cheetomoskeeto’s orb smasher tutorial at http://www.youtube.com/watch?v=6TsDdLY7VXk and am having an issue adding a delay when i want the listener to receive variables…

placing random orbs on the screen is no issue, get a nice delay of whatever i want using timer.performWithDelay

tmr = timer.performWithDelay(200, spawnEgg, total\_eggs);  

but when i try to place them in a grid it works, but without the intended delay.

for i = 1, egg\_columns do  
 for j = 1, egg\_rows do  
 tmr = timer.performWithDelay(200, spawnEgg(i,j), 1);  
 end  
end  

so 2 questions…

  • can i not pass variables from timer.performWithDelay?
  • if not is there another delay method? [import]uid: 24493 topic_id: 6437 reply_id: 306437[/import]

When you put () that calls the function immediately.

You may be able to work around this however by making the parameters into properties of the timer. I’m not at home to test if this works, but you might try:

[code]
local function spawnEgg(event)
print(event.msg)
end

local tmr = timer.performWithDelay(200, spawnEgg, 1)
tmr.msg = “This is a test.”
[/code] [import]uid: 12108 topic_id: 6437 reply_id: 22264[/import]

thank you! i will try it after work. [import]uid: 24493 topic_id: 6437 reply_id: 22386[/import]

no joy.

any other methods for pausing?

thanks again.

[import]uid: 24493 topic_id: 6437 reply_id: 22637[/import]

Hello, maybe this can help you:

Using Closures

Regards.
Francisco. [import]uid: 11749 topic_id: 6437 reply_id: 22668[/import]

you could also use something like
[lua]local i,j = 0,0
local tmr

local egg_rows=5
local egg_cols=5

function spawnEgg()

print(“spawn egg at “…i…”,”…j)

j=j+1

if(j==egg_rows) then

i=i+1
j=0

if(i==egg_cols) then timer.cancel(tmr) end

end

end

tmr = timer.performWithDelay(1, spawnEgg, -1)[/lua] [import]uid: 6645 topic_id: 6437 reply_id: 22671[/import]

jmp909 you rock!

thanks all for your replies. working! woohoo! :slight_smile: [import]uid: 24493 topic_id: 6437 reply_id: 23002[/import]

ok this is probably quite bad because it creates multiple timers but i’m just showing how to pass variables on a timer. I wouldn’t use this method

[lua]function spawnEgg(event)

local t = event.source – the timer
local params = t.params – our assigned variable
print(params.i,params.j)
print(t.blah) – “whatever”

end

for i = 1, egg_columns do
for j = 1, egg_rows do
local delay = 200 * (((i-1)*egg_rows) + j)
local tmr = timer.performWithDelay(delay, spawnEgg, 1);
tmr.params = {i=i, j=j}-- assign a variable to the timer, along with some values
tmr.blah = “whatever”
end

end[/lua]

[import]uid: 6645 topic_id: 6437 reply_id: 23168[/import]

JMP: I think my hacked android tablet would explode with all those timers! Have you tried that in the simulator? I’ll give it a shot tonight. Thanks! [import]uid: 24493 topic_id: 6437 reply_id: 23215[/import]

i’m saying don’t do it.

just an explanation of how to pass variables to a timer complete function.

[import]uid: 6645 topic_id: 6437 reply_id: 23220[/import]