Pausing and resuming timers

hey guys,

I’m having a problem with timers. Sometimes when a timer is paused and resumed the createShot() function delay a bit to resume.
This is a bit about my code :

[lua] createShot = function()
local bullet = display.newImage(bullets, “bullet.png”)
bullet.x = player.x
bullet.y = player.y - (player.height * .5) - 8
bullet.myName = “bullet”
physics.addBody(bullet, {filter = bullet_CollisionFilter} )
bullet.collision = collisionEvent
bullet:addEventListener(“collision”, bullet)
audio.play(shotSound)
end

createBack = function(imageName)
local imageBack = display.newImage(backs, imageName…".png")
imageBack.x = createRandom(imageBack.width, w - imageBack.width)
imageBack.y = -imageBack.height
imageBack.myName = imageName
physics.addBody(imageBack, “dynamic”, {filter = back_CollisionFilter})
end

timers[1] = timer.performWithDelay(150, createShot, 0)

timers[6] = timer.performWithDelay(1000,
function()
createBack(“starSmall”)
end, 0)

timers[7] = timer.performWithDelay(3000,
function()
createBack(“starBig”)
end, 0)

timers[8] = timer.performWithDelay(3500,
function()
createBack(“speedLine”)
end, 0)

timersFunction = function(timerType)
if timerType == “pause” then
timer.pause(timers[1])
timer.pause(timers[6])
timer.pause(timers[7])
timer.pause(timers[8])
elseif timerType == “resume” then
timer.resume(timers[1])
timer.resume(timers[6])
timer.resume(timers[7])
timer.resume(timers[8])
elseif timerType == “cancel” then
for j=1, #timers do
if timers[j] ~= nil then
timer.cancel(timers[j])
timers[j] = nil
end
end

timers = nil
end
end

pauseGame = function(e)
if e.phase == “began” then
if pause then
timersFunction(“pause”)
physics.pause()
pause = false
else
timersFunction(“resume”)
physics.start()
pause = true
end
end
end[/lua]

Still there is a function to move the bullet and the background but i think it is enough to understand my problem.

someone can help me ?
thanks. [import]uid: 95474 topic_id: 33176 reply_id: 333176[/import]

there is a library that solves this bug :
http://developer.coronalabs.com/code/pausable-timers-and-transitions-speed-adjustment

i’m sorry i didn’t know ^^

But now the code there is a bit of memory leak. I’m trying improve it.

Good bye, [import]uid: 95474 topic_id: 33176 reply_id: 132464[/import]

there is a library that solves this bug :
http://developer.coronalabs.com/code/pausable-timers-and-transitions-speed-adjustment

i’m sorry i didn’t know ^^

But now the code there is a bit of memory leak. I’m trying improve it.

Good bye, [import]uid: 95474 topic_id: 33176 reply_id: 132464[/import]

Hey sorry I only took a glance at your code but are you sure it isn’t you creating stuff with the same variable name that’s causing memory leaks?

You should be creating tables. Um as in

bullet = {}  
-- then everytime you create a bullet you do something like  
bullet[i] = display.new ...  
i = i + 1  

That way each bullet has a designated number assigned to it so you can make sure to delete every single one of them and not have any extras.

hope this makes sense, been up all night. might be wrong about your memory leak but should help you with your game either way

same thing goes for the imageback btw depending on how you handle their deletion [import]uid: 77199 topic_id: 33176 reply_id: 132945[/import]

i’m deleting the images by groups …
like this :

[lua]for j=1, backs.numChildren do
if backs[j] ~= nil then
if backs[j].myName == “starSmall” then
backs[j]:setLinearVelocity(0, 50)
elseif backs[j].myName == “starBig” then
backs[j]:setLinearVelocity(0, 80)
elseif backs[j].myName == “speedLine” then
backs[j]:setLinearVelocity(0, 100)
end

if backs[j].y > h - (backs[j].height * .5) then
display.remove(backs[j])
backs[j] = nil
end
end
end[/lua] [import]uid: 95474 topic_id: 33176 reply_id: 132993[/import]

Hey sorry I only took a glance at your code but are you sure it isn’t you creating stuff with the same variable name that’s causing memory leaks?

You should be creating tables. Um as in

bullet = {}  
-- then everytime you create a bullet you do something like  
bullet[i] = display.new ...  
i = i + 1  

That way each bullet has a designated number assigned to it so you can make sure to delete every single one of them and not have any extras.

hope this makes sense, been up all night. might be wrong about your memory leak but should help you with your game either way

same thing goes for the imageback btw depending on how you handle their deletion [import]uid: 77199 topic_id: 33176 reply_id: 132945[/import]

i’m deleting the images by groups …
like this :

[lua]for j=1, backs.numChildren do
if backs[j] ~= nil then
if backs[j].myName == “starSmall” then
backs[j]:setLinearVelocity(0, 50)
elseif backs[j].myName == “starBig” then
backs[j]:setLinearVelocity(0, 80)
elseif backs[j].myName == “speedLine” then
backs[j]:setLinearVelocity(0, 100)
end

if backs[j].y > h - (backs[j].height * .5) then
display.remove(backs[j])
backs[j] = nil
end
end
end[/lua] [import]uid: 95474 topic_id: 33176 reply_id: 132993[/import]