Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Question to timer
Started by d.mach Feb 11 2019 08:15 AM

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

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 784 posts
  • Corona SDK

I wonder how to add a timer using the correct time to fit an already started timer until end. So they can end at the exact same time.

 

For example:

I start a timer with performWithDelay(21020,destroy,1)

 

And then I want to create new timers for example when a button is pushed, BUT the new timers should use the rest of the left time from the first timer, so all the timers end at the exact same time.

 

How can I do this?

 



[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 726 posts
  • Corona SDK

I don't think that can be done only using the timer api. You could solve this by saving time stamps, but for them to finish at the exact point, you probably need to cancel involved timers at breakpoints and restart them from that point for a duration that you can calculate.

[TOPIC: post.html]
#3

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 385 posts
  • Corona SDK

Try

local delay = 10500 -- 10.5 sec
local startTime
local index=1

local function printTime( event )
  local params = event.source.params
  local d, st, id = params.delay, params.startTime, params.id


  print( 'id='                .. id ..
        ' startTime='         .. st ..
        ' delay='             .. d ..
        ' system.getTimer()=' .. system.getTimer()
   )
end

local buttonRunNewTimer = display.newRect(
                                display.contentCenterX,
                                display.contentCenterY,
                                100,
                                100
                              )

buttonRunNewTimer.touch = function( self, event )
  local phase = event.phase

  if phase == 'began' then
    local st = system.getTimer()
    local d  = delay + startTime - st
    local t  = timer.performWithDelay( d, printTime, 1 )

    index = index + 1
    t.params = { id=index, startTime=st, delay=d }
  end
end

buttonRunNewTimer:addEventListener( "touch" )

startTime = system.getTimer()
local t = timer.performWithDelay( delay, printTime, 1 )
t.params = { id=index, startTime=startTime, delay=delay }

It is not perfect.

 

Have a nice  day:)

 

 

ldurniat



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 524 posts
  • Corona SDK

I would do this by storing the time that the original timer started and then calculating the other timers around that, i.e.

 

local maxTime = 21020
local timers = {}

local startTime = system.getTimer()
timers[1] = timer.performWithDelay( maxTime, destroy )

-- some time later when you create a new timer
timers[2] = timer.performWithDelay( maxTime-(system.getTimer()-startTime), destroy )

(system.getTimer()-startTime) simply gives you the amount of time that has passed since the startTime was taken, i.e. when timers[1] was fired. When you then subtract this time from the maxTime that you gave the initial function, they'll stop at the same time.

If you use the method above, you could write a safety function that first checks if maxTime-(system.getTimer()-startTime) is actually positive and if it its, then creates a new timer. I haven't yet tried what happens if you try to create a timer with negative time. :P



[TOPIC: post.html]
#5

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 784 posts
  • Corona SDK

Thank you all for your detailed quick help and response! Much appreciated! :)

Awesome!



[TOPIC: post.html]
#6

Yoger Games

[GLOBAL: userInfoPane.html]
Yoger Games
  • Contributor

  • 103 posts
  • Corona SDK

Why do you want the timers to execute at the exact same time? Does Corona guarantuee that multiple timers will trigger at the exact same time?

 

Maybe better to call the desired code from end of first timer ?




[topic_controls]
[/topic_controls]