Increasing Timer Through Collide-Able Object

I need help in setting up a collision on an object which interacts with the timer countdown by increasing the current time.

As I’m still grasping coding I am stuck in trying to get this to work. What I want to happen is the player collides into the star and the time increases by 5 secs.

Here is my set up.

Timer 

function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0 and tonumber(collectedN.text) \>= tonumber(requiredN.text)) then alert('win') elseif(timeLimit==0 and tonumber(collectedN.text) \< tonumber(requiredN.text)) then alert('lose') end end timer.performWithDelay(1000,timerDown,timeLimit)

Star collision

function ship:collision(event) if (event.other.name == 'star') then timeLeft.text = tostring(tonumber(timeLeft.text) + 5) timeLeft.text = timeLimit --removes star event.other:removeSelf(star) scoreAnim = display.newText('+10', ship.x, ship.y-10, native.systemFontBold, 16) transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end}) scoreAnim:setTextColor(0,255,12) Runtime:removeEventListener("enterFrame", star) --increases score collectedN.text = tostring(tonumber(collectedN.text) + 1) print('star successful')

Hi @dip,

Since you can’t just “add” time to a running timer, I suggest that you create a timer assigned to a variable. When the collision happens, you cancel that timer, read the time that was remaining on it, and then restart it (under the same variable/handle) with an additional 5 seconds. Like follows:

[lua]

local function onTimerFinish( event )

   --whatever happens when timer expends completely

end

local myTimer = timer.performWithDelay( 4000, onTimerFinish )

local function addTime( event )
   local timeRemaining, numberOfIterationsRemaining = timer.cancel( myTimer )

   myTimer = timer.performWithDelay( timeRemaining+5000, onTimerFinish )
end
timer.performWithDelay( 2000, addTime )  --in your code, call the “addTime” function in your collision

[/lua]

Hope this helps!

Brent

Hi Brent thank you for the reply. However I still cannot get this to work properly, it looks like the timer increases by 5 then goes back to counting back from where it was instead of cancelling and about the timeLeft variable.

Do I need to remove my 'timerDown function completely? When I do that it and use the ‘myTimer’ one instead it just doesn’t count down. 

Sorry if its a silly question, im still learning this all. 

Hi @dip,

There are no “silly” questions when it comes to code, except those that are never asked (and thus not resolved). :slight_smile:

Remember that the timer example I showed is independent of any kind of “visual” timer you’re showing in text. If you need that, it might be useful to read the following tutorial which discusses how to work with time in general:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

And since you’re new to Corona, I should cite some useful pages with links to various Corona-related resources and tutorials:

http://docs.coronalabs.com/guide/index.html

http://www.tandgapps.co.uk/resources/coronalabs-resources/

Best regards,

Brent

Hi @dip,

Since you can’t just “add” time to a running timer, I suggest that you create a timer assigned to a variable. When the collision happens, you cancel that timer, read the time that was remaining on it, and then restart it (under the same variable/handle) with an additional 5 seconds. Like follows:

[lua]

local function onTimerFinish( event )

   --whatever happens when timer expends completely

end

local myTimer = timer.performWithDelay( 4000, onTimerFinish )

local function addTime( event )
   local timeRemaining, numberOfIterationsRemaining = timer.cancel( myTimer )

   myTimer = timer.performWithDelay( timeRemaining+5000, onTimerFinish )
end
timer.performWithDelay( 2000, addTime )  --in your code, call the “addTime” function in your collision

[/lua]

Hope this helps!

Brent

Hi Brent thank you for the reply. However I still cannot get this to work properly, it looks like the timer increases by 5 then goes back to counting back from where it was instead of cancelling and about the timeLeft variable.

Do I need to remove my 'timerDown function completely? When I do that it and use the ‘myTimer’ one instead it just doesn’t count down. 

Sorry if its a silly question, im still learning this all. 

Hi @dip,

There are no “silly” questions when it comes to code, except those that are never asked (and thus not resolved). :slight_smile:

Remember that the timer example I showed is independent of any kind of “visual” timer you’re showing in text. If you need that, it might be useful to read the following tutorial which discusses how to work with time in general:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

And since you’re new to Corona, I should cite some useful pages with links to various Corona-related resources and tutorials:

http://docs.coronalabs.com/guide/index.html

http://www.tandgapps.co.uk/resources/coronalabs-resources/

Best regards,

Brent