How can I stop and start the game timer

Hello guys, I have a problem developing my game.

I put a pause button , for pause physics and every events, I can make the timer pause, but, when I touch the pause button again, the time does  not start , I cant make it work well

here is may code:

local onPauseTouch = function( event )

timer.pause(gametmr)

physics.pause()

pauseBtn.isVisible = false

pausBt.isVisible = true

end

pauseBtn:addEventListener(“touch”, onPauseTouch)


function star(event)

physics.start(true)

pauseBtn.isVisible = true

pausBt.isVisible = false

end

pausBt:addEventListener(“touch”, star)

Please help me with this.

Thanks.

Why don’t you use storyboard - it will be very easy to pause timers.

http://docs.coronalabs.com/api/library/storyboard/index.html

http://www.coronalabs.com/blog/2012/08/21/storyboard-basic-usage/

Without other code it is difficult to say - what excactly is wrong.

UPD: Oh, I see. When you press pausBt you goes to star function, which has no timer.resume()

Hello, I dont understand well the storyboard , I use the director class, and is very easy tu use.

How easy is the storyboard?

The most confusing thing in storyboard is a purgeScene(). I spend a day to try to understand where excactly should I put these comand. But nevertheless it is very easy to learn a basic principles of it. You also can see an example in Corona simulator.

First of all you need create the scenes - game and pause/menu. You can find a template in the example. All objects you should put in the createScene(), and all Listeners in enterScene(). remove these listeners in the exitScene(). And pause button touch event should be the next:

function onPauseTouch (event) print ("--\> pause button was pressed") storyboard.purgeScene ("game") storyboard.gotoScene ("menu") end

It is a little tricky, because for my opinion, on the first place we should go to the new scene and next to purge previous. But if we do this, we will get error in createScene(). I hope it will help you!

If you want to control a timer you have to have a handler for it like

local gameTimer = timer.performWithDelay( --[[your params goes in here–]] )

This has to be declared before you call the function, else the function doesn´t know what to do

now, when you give gameTimer instructions with a closing “return true” it will work

(when you return the result you also have the ability to cancel the object for proper garbage handeling…if you´re not using stuff like storyboard)

Hope this helps you out

Hello, thanks for your help, but I already solve my problem, I resolve this function with (timer.restart) , but the code you gave me

can work to stop time for a few seconds?.

please you can give me an example for this, pause time for a few seconds.

Thanks

Of course

local myFunction = {}

local myTimer

myFunction = function()

   print( “the chunk of code you like to fire after some seconds” )

end

myTimer = timer.performWithDelay( 2000, myFunction, 1 ) --result: runs myFunction 1 time after a delay of 2 seconds

Why don’t you use storyboard - it will be very easy to pause timers.

http://docs.coronalabs.com/api/library/storyboard/index.html

http://www.coronalabs.com/blog/2012/08/21/storyboard-basic-usage/

Without other code it is difficult to say - what excactly is wrong.

UPD: Oh, I see. When you press pausBt you goes to star function, which has no timer.resume()

Hello, I dont understand well the storyboard , I use the director class, and is very easy tu use.

How easy is the storyboard?

The most confusing thing in storyboard is a purgeScene(). I spend a day to try to understand where excactly should I put these comand. But nevertheless it is very easy to learn a basic principles of it. You also can see an example in Corona simulator.

First of all you need create the scenes - game and pause/menu. You can find a template in the example. All objects you should put in the createScene(), and all Listeners in enterScene(). remove these listeners in the exitScene(). And pause button touch event should be the next:

function onPauseTouch (event) print ("--\> pause button was pressed") storyboard.purgeScene ("game") storyboard.gotoScene ("menu") end

It is a little tricky, because for my opinion, on the first place we should go to the new scene and next to purge previous. But if we do this, we will get error in createScene(). I hope it will help you!

If you want to control a timer you have to have a handler for it like

local gameTimer = timer.performWithDelay( --[[your params goes in here–]] )

This has to be declared before you call the function, else the function doesn´t know what to do

now, when you give gameTimer instructions with a closing “return true” it will work

(when you return the result you also have the ability to cancel the object for proper garbage handeling…if you´re not using stuff like storyboard)

Hope this helps you out

Hello, thanks for your help, but I already solve my problem, I resolve this function with (timer.restart) , but the code you gave me

can work to stop time for a few seconds?.

please you can give me an example for this, pause time for a few seconds.

Thanks

Of course

local myFunction = {}

local myTimer

myFunction = function()

   print( “the chunk of code you like to fire after some seconds” )

end

myTimer = timer.performWithDelay( 2000, myFunction, 1 ) --result: runs myFunction 1 time after a delay of 2 seconds