Timer for each individual object

Hello everyone,

I’m trying to spawn a bunch of objects each with their own individual timers that count down with the remaining time shown by a text string tied to the object. Is there any way to have each new object have its own individual timer?

Thanks. [import]uid: 47722 topic_id: 29228 reply_id: 329228[/import]

Yes - spawn the objects in a table and then either create the timers like obj.timer = timer or store the timers in a table also so that the numbers correspond. (I believe either should work fine though haven’t tested the lesser.) [import]uid: 52491 topic_id: 29228 reply_id: 117545[/import]

I’m not quite sure how to spawn objects in a table. Is it somewhat similar to this?

local ball = []

ball = display.newImsgeRect…

Thank you [import]uid: 47722 topic_id: 29228 reply_id: 117609[/import]

Spawning objects in a table is pretty easy. Something like this:

balls = {}  
  
for i=1, 10 do  
 local ball = display.newImage(foo)  
 ball.tmr = timer.performWithDelay( delay, listener [, iterations] )  
 balls[i] = ball  
end  

I created a video tutorial that talks about creating objects in a loop. You can find it here:

http://gamedevnation.com/game-development/billions-of-stars/

It doesn’t talk about adding timers, but with that snippet above as a guide you should be good.

Jay

[import]uid: 9440 topic_id: 29228 reply_id: 117618[/import]

Hello everyone,

Thank you Jay for the great advice and video. Most of my code is now working. I just have one more question. If I have a timer that looks like ball.tmr, how do I pause/resume it when a button is pressed? Normally, this type of thing works when I use timer.pause and timer.resume, but I keep getting an error that says it can’t find/index global value ball. Any help would be appreciated.

Thank you. [import]uid: 47722 topic_id: 29228 reply_id: 117957[/import]