Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Cancel timers with a function
Started by dodi_games Feb 15 2019 05:01 PM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 365 posts
  • Corona SDK

Hi guys! I wanted to ask if this technique is a good idea. I'm using enough timers to create a game and I need to group them in a table and cancel all of them in scene:hide with a function.

 

create a table

--timers
local t = {}  --local table for all timers

use some timers

t.tmr1 = timer.performWithDelay(...)
t.tmr2 = timer.performWithDelay(...)
t.tmr3 = timer.performWithDelay(...)
…
t.tmr17 = timer.performWithDelay(...)
etc

I think that if I am using a table I have a key / values ​​which I can use in the following way to cancel everything at once.

 

cancel all timers at once

--cancel all timers
local function cancelAllTimers()
  for k, v in pairs(t) do
    timer.cancel(v)
  end
  k = nil
end

I do not receive any errors on the console or on the devices. I just want to know if doing this is a good practice?

 

***Sometimes I feel doubt about what I do and would like someone with more experience tell me if I'm on the right way or I should continue evaluating other methods***

 

Thanks in advance

DoDi

 

 



[TOPIC: post.html]
#2

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 466 posts
  • Corona SDK

Hi DoDi,

There’s no problem with that. It’s not unlike what I do with my “timer2” module, that you may want to check out. Adds support for tagging timers so you can cancel/pause/resume all timers for a given tag simultaneously, or cancel/pause/resume all timers regardless of tag. It’s on GitHub: https://github.com/schroederapps/corona-timer2
  • horacebury and dodi_games like this

[TOPIC: post.html]
#3

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 365 posts
  • Corona SDK

@schroederapps
Thanks for your quick response, I'll take a look!


[topic_controls]
[/topic_controls]