Problem with loop and function

Hello i have this code:

It basicly make this:

these 2 rng flames on those torches

8cf7416cae.png

local function background() local y=math.random(850,870) local x=math.random(60,90) local p=math.random(30,50) local r=math.random(80,160) local y1=math.random(850,870) local x1=math.random(60,90) local p1=math.random(30,50) local r1=math.random(80,160) local s=-1 local function backgroundanim() local rect = display.newRect(x,y,p,p) rect:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) local rect1 = display.newRect(x1+560,y1,p1,p1) rect1:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) groupmenu:insert(1,rect) groupmenu:insert(1,rect1) transition.to( rect, { time=4000, rotation=r}) transition.to( rect, { time=5000, xScale=s, yScale=s}) transition.to( rect, { time=1000, delay=4000, alpha=1}) transition.to( rect, { time=5000, y=y-600}) transition.to( rect1, { time=4000, rotation=r}) transition.to( rect1, { time=5000, xScale=s, yScale=s}) transition.to( rect1, { time=1000, delay=4000, alpha=1}) transition.to( rect1, { time=5000, y=y-600}) timer.performWithDelay(2500,function() display.remove(rect) end) timer.performWithDelay(2500,function() display.remove(rect1) end) y=math.random(850,870) x=math.random(60,90) p=math.random(30,50) r=math.random(120,1660) y1=math.random(850,870) x1=math.random(60,90) p1=math.random(30,50) r1=math.random(120,1660) end backgroundanim() end

Then i call this whole function by using 

 timer.performWithDelay( 70, background, 0)

so there is many rectangles making “flame” infinite time. this function and timer are in my menu() funcion with torches and butons, i call it at the botom of my main.lua file with simply menu(). now what i’m trying to do is to distable this function when i tap the options button, i’ve achieved this by adding at the top of my menu() function :

local flamefunction=true

then at options “touch” event adding:

 local function taponoptions( event ) if (event.phase == "began") then flamefunction=false ( rest of the code deleting menu buttons) end

and then i added this to function with flames.

if (flamefunction == false) then display.remove(rect) display.remove(rect1) end

But when i use my back button in options which is deleting options buttons and making my menu() again, there is 2x (twice) as meny flame rectangles and when i keep doing it they multiply always by 2… i don’t know how to stop timer.performWithDelay() or to distable this flames function when i press options button and resume it when i use return button 

but without timer.perfomWithDelay() i dont know how can i do it. thou i can’t use

while ( var ==true ) do background() end 

Becouse there is no delay betwen spawning rectangles so it crashes.

Can’t either spawn many rectanles or check if flamefunction is false or true.

I dont know what to do.

maybe anyone who understood this whole post will help me! sorry for my english by the way!

But when i use my back button in options which is deleting options buttons and making my menu() again, there is 2x (twice) as meny flame rectangles and when i keep doing it they multiply always by 2… i don’t know how to stop timer.performWithDelay() or to distable this flames function when i press options button and resume it when i use return button

timer.performWithDelay returns a reference to the timer object, you can store that somewhere then stop it with timer.cancel

if you don’t pass a specific timer to the cancel function it cancels all of them.
Since all your currently existing rectangles are inserted into groupmenu if you’re removing that then all of them will be removed and you can just call timer.cancel() to clean up all the timers (if you’re not removing groupmenu then you’d need to be more selective about canceling the timers)

You might also take a look at this. Would be perfect for what you want:

https://docs.coronalabs.com/api/type/EmitterObject/index.html

Try this cleanup function.  I added a delay to it so you can see it working but you would call it when you exit the menu scene:

local groupmenu = display.newGroup() local function background() local y=math.random(850,870) local x=math.random(60,90) local p=math.random(30,50) local r=math.random(80,160) local y1=math.random(850,870) local x1=math.random(60,90) local p1=math.random(30,50) local r1=math.random(80,160) local s=-1 local function backgroundanim() local rect = display.newRect(x,y,p,p) rect:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) local rect1 = display.newRect(x1+560,y1,p1,p1) rect1:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) groupmenu:insert(1,rect) groupmenu:insert(1,rect1) transition.to( rect, { time=4000, rotation=r}) transition.to( rect, { time=5000, xScale=s, yScale=s}) transition.to( rect, { time=1000, delay=4000, alpha=1}) transition.to( rect, { time=5000, y=y-600}) transition.to( rect1, { time=4000, rotation=r}) transition.to( rect1, { time=5000, xScale=s, yScale=s}) transition.to( rect1, { time=1000, delay=4000, alpha=1}) transition.to( rect1, { time=5000, y=y-600}) timer.performWithDelay(2500, function() display.remove(rect) end) timer.performWithDelay(2500,function() display.remove(rect1) end) y=math.random(850,870) x=math.random(60,90) p=math.random(30,50) r=math.random(120,1660) y1=math.random(850,870) x1=math.random(60,90) p1=math.random(30,50) r1=math.random(120,1660) end backgroundanim() end local backgroundTimer = timer.performWithDelay( 70, background, 0) local function cleanup() --Stop the background timer timer.cancel(backgroundTimer) backgroundTimer = nil --Cancel transitions transition.cancel() --Clean up the group objects for i = groupmenu.numChildren, 1, -1 do groupmenu[i]:removeSelf( ) groupmenu[i] = nil end groupmenu:removeSelf() groupmenu = nil end timer.performWithDelay( 2000, cleanup)

But when i use my back button in options which is deleting options buttons and making my menu() again, there is 2x (twice) as meny flame rectangles and when i keep doing it they multiply always by 2… i don’t know how to stop timer.performWithDelay() or to distable this flames function when i press options button and resume it when i use return button

timer.performWithDelay returns a reference to the timer object, you can store that somewhere then stop it with timer.cancel

if you don’t pass a specific timer to the cancel function it cancels all of them.
Since all your currently existing rectangles are inserted into groupmenu if you’re removing that then all of them will be removed and you can just call timer.cancel() to clean up all the timers (if you’re not removing groupmenu then you’d need to be more selective about canceling the timers)

You might also take a look at this. Would be perfect for what you want:

https://docs.coronalabs.com/api/type/EmitterObject/index.html

Try this cleanup function.  I added a delay to it so you can see it working but you would call it when you exit the menu scene:

local groupmenu = display.newGroup() local function background() local y=math.random(850,870) local x=math.random(60,90) local p=math.random(30,50) local r=math.random(80,160) local y1=math.random(850,870) local x1=math.random(60,90) local p1=math.random(30,50) local r1=math.random(80,160) local s=-1 local function backgroundanim() local rect = display.newRect(x,y,p,p) rect:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) local rect1 = display.newRect(x1+560,y1,p1,p1) rect1:setFillColor( math.random(80, 100)/100, math.random(10, 40)/100, math.random(0, 0)/100 ) groupmenu:insert(1,rect) groupmenu:insert(1,rect1) transition.to( rect, { time=4000, rotation=r}) transition.to( rect, { time=5000, xScale=s, yScale=s}) transition.to( rect, { time=1000, delay=4000, alpha=1}) transition.to( rect, { time=5000, y=y-600}) transition.to( rect1, { time=4000, rotation=r}) transition.to( rect1, { time=5000, xScale=s, yScale=s}) transition.to( rect1, { time=1000, delay=4000, alpha=1}) transition.to( rect1, { time=5000, y=y-600}) timer.performWithDelay(2500, function() display.remove(rect) end) timer.performWithDelay(2500,function() display.remove(rect1) end) y=math.random(850,870) x=math.random(60,90) p=math.random(30,50) r=math.random(120,1660) y1=math.random(850,870) x1=math.random(60,90) p1=math.random(30,50) r1=math.random(120,1660) end backgroundanim() end local backgroundTimer = timer.performWithDelay( 70, background, 0) local function cleanup() --Stop the background timer timer.cancel(backgroundTimer) backgroundTimer = nil --Cancel transitions transition.cancel() --Clean up the group objects for i = groupmenu.numChildren, 1, -1 do groupmenu[i]:removeSelf( ) groupmenu[i] = nil end groupmenu:removeSelf() groupmenu = nil end timer.performWithDelay( 2000, cleanup)