timer.performWithDelay..... help

Hi
I have little problem… I have a tree with fruits and I want them to fall down each after plus minus 1 sec.
so this is what I done :

  
local n = 600  
local showOranges = function()  
local fruits = display.newImage("frutit.png")  
 local time = n   
 orangeGroup:insert(fruits)  
fruits.collision = floorCollision  
fruits:addEventListener("collision", fruits)  
theTimer7 = timer.performWithDelay(time, function()   
physics.addBody(fruits, "dynamic", {density = 0.1, friction = 0.1, radius = 22, bounce = 0.2, filter = {categoryBits = 2, maskBits = 1} })  
fruits:addEventListener("touch", function(event) cutFruit(fruits) end )end)  
n = n + 600  
end  
  
timer.performWithDelay(1, showOranges, #orangesPositions )  
  

Its works ok but I can not stop it. I mean if I want to stop game after 5 oranges theTimer7 is still working, ofcourse I’am canceling it.
I’am changing scene and theTimer7 is still trying to add physics body to another fruit. and I’ve got a lot errors.
I understand why but maybe is there a simple way to draw objects and then add dynamic physics and event listener one by one.
[import]uid: 13156 topic_id: 13179 reply_id: 313179[/import]

I am not exactly sure what you want to do. If you want to make your timer stop after a certain amount of loops then at the end of

theTimer7 = timer.performWithDelay(time, function()
put the number of times you want it to loop. like this
theTimer7 = timer.performWithDelay(time, function(), number of loops)

I hope this helps :slight_smile:

Boogleybones [import]uid: 62404 topic_id: 13179 reply_id: 48390[/import]

Yes you are right but I don’t know how many loops it’s gonna be because its depends of how many oranges user cut.
Right now loops = #orangesPositions … All oranges.
If I make if theTimer7 timer.cancel(theTimer7) end after user cut for 7 oranges it’s still trying to add body to the rest of oranges. [import]uid: 13156 topic_id: 13179 reply_id: 48415[/import]

any ideas ? :slight_smile: [import]uid: 13156 topic_id: 13179 reply_id: 49449[/import]