Passing parameters from timers?

I am trying to make an egg spawn every XX seconds from where the user clicks the screen. I want the player to be able to make multiple spawners. My problem is that when the player creates more than one, the eggs only spawn from the last place clicked, not from their original location. So think of the game Plants vs. Zombies and how the plants shoot a seed every XX seconds from their location. Thats basically what I’m trying to do. Here is part of my code:
[lua]function shootEgg(tX, tY, power)
– Spawn egg
local egg = display.newCircle(tX, tY, 10)

– Add bullet physics
physics.addBody( egg, { density= 10.0, friction= 0.3, bounce=0.00 } )
egg.isBullet = true
egg:applyForce( power*20, 0, egg.x, egg.y )
return egg
end

function 123()
timers[1][2] = timer.performWithDelay(2000, shootEgg(x_here, y_here, power_here), 0)
end[/lua]

So it works correctly for the first iteration, but does not continue afterwards. If I take the parameters out of the timer, it iterates through fine but doesn’t carry the parameters which I need. [import]uid: 63320 topic_id: 15899 reply_id: 315899[/import]

you need to give each place touched a unique name

tCount = 1
touch = {}

touch[tCount] = { event.x,event.y }
tCount = tCount + 1

then cycle though the touch table for each touch x & y
[import]uid: 7911 topic_id: 15899 reply_id: 58814[/import]

[lua]function shootEgg(tX, tY, power)
– Spawn egg
local egg = display.newCircle(tX, tY, 10)

– Add bullet physics
physics.addBody( egg, { density= 10.0, friction= 0.3, bounce=0.00 } )
egg.isBullet = true
egg:applyForce( power*20, 0, egg.x, egg.y )
return egg
end

local tempFunction = function()
shootEgg(x_here, y_here, power_here)
end

function 123()
timers[1][2] = timer.performWithDelay(2000, tempFunction, 0)
end[/lua]

This should do it! If you use shootEgg(x_here, y_here, power_here) in the timer, the timer takes the returned value of shootEgg as its value. Hence it is executed only once! :slight_smile:

Hope that helps!
[import]uid: 64174 topic_id: 15899 reply_id: 58818[/import]

Here is how i handle passing vars with timers. Basically i keep all my timers attached the related objects.

[code]
function gunObject:shootEgg(event)
– Spawn egg
local Bullet = display.newCircle(gunObject.x , gunObject.y, 10 )
– Add bullet physics
timer.performWithDelay(1,function() – i normaly add a frame delay before spawning any type of physics object.
physics.addBody( Bullet, { density= 10.0, friction= 0.3, bounce=0.00 } )
Bullet.isBullet = true
Bullet:applyForce( gunObject.power*20, 0, gunObject.targetX, gunObject.targetY )
return Bullet
end

end

function shootGun(EggObject)

local gunObject = getGunObject() – and/or create your gun object here
gunObject.targetX = EggObject.x
gunObject.targetY = EggObject.y
gunObject.power = 100
gunObject.timer = timer.performWithDelay( 2000, gunObject, 0 )

end
[import]uid: 7177 topic_id: 15899 reply_id: 58820[/import]

Thanks for your replies! The problem is I need to have multiple timers launching the same function but with different parameters. For example:

timer1: x = 50, y = 50, size = 10
timer2: x = 27, y = 56, size = 20
timer3: x = 38, y = 90, power = 10
[lua]function 123()
newCircle(x,y,size) --All the timers run this function but I need each of them to carry different
–parameters. When I use the methods mentioned above, all the timers
–use the values of the last called timer.
end[/lua]

Is what I am trying to do achievable? In plants vs. zombies, the plants launch their seeds on a timed interval and at their location. Not at the location of the last plant built. [import]uid: 63320 topic_id: 15899 reply_id: 58961[/import]

Something like this?

[lua]

timer1 = timer.performWithDelay(2000, function() shootEgg(50,50,10), 0) end,0)

timer2 = timer.performWithDelay(2000, function() shootEgg(27,56,20), 0) end,0)

timer3 = timer.performWithDelay(2000, function() shootEgg(38,90,10), 0) end,0)[/lua] [import]uid: 64174 topic_id: 15899 reply_id: 58967[/import]

Thanks again for the reply! I would expect that to work but the same issue still exists, all the eggs spawn from the last clicked location. Instead of them spawning from the original location AND fro each subsequent location clicked, they ALL spawn from the last location clicked. [import]uid: 63320 topic_id: 15899 reply_id: 58973[/import]

The left is what I’m trying to do. The right is my result.

As you can see, all the eggs shoot from the last clicked location instead of from it’s correct location like on the left. [import]uid: 63320 topic_id: 15899 reply_id: 58980[/import]

Ok so after rewriting all of my code I finally managed to get it working. Thanks for all the replies and help! [import]uid: 63320 topic_id: 15899 reply_id: 58996[/import]