I want to put a timer in my app but the documentation on here isn’t helping at all for me . I want to put a timer in the game and when the user dies and goes to the restart screen it says how long they lasted but I couldn’t find tutorials on it on here or YouTube . Any help ?
I don’t think he means a score counter, he specifically said he wants the time that the user was in the game.
brandont264, the easiest way I can think of is to record the time that the user starts the game, then when the game ends the length of time will be:
timeSpentInGame = endTime - startTime
I don’t know how your code is set out (e.g. if you are using scenes with enter/exit functions) but here is the gist of what to do to get the time spent in the level:
--this must NOT be local to the start game function, as you will need to access it in the exit game function local startTime local function startGame() --get time that the game started startTime = system.getTimer() end local function endGame() --get time that the game ended local endTime = system.getTimer() --calculate time in milliseconds spent in game local timeSpentInGame = endTime - startTime --convert to seconds if that is useful for you local secondsInGame = timeSpentInGame / 1000 end
Also if you want to show a timer during the gameplay itself, just do the same thing but calculate the timeSpentInGame in an enterFrame listener:
--this must NOT be local to the start game function, as you will need to access it in the exit game function local startTime --create a text object to display the timer local myTimeText = display.newText("0", 100, 100, native.systemFont, 50) local function startGame() --get time that the game started startTime = system.getTimer() end local function update() --get the current time local now = system.getTimer() --calculate time in milliseconds spent in game local timeSpentInGame = now - startTime --convert to seconds if that is useful for you local secondsInGame = timeSpentInGame / 1000 --update the text object to show the number of seconds myTimeText.text = secondsInGame end startGame() Runtime:addEventListener("enterFrame", update)
What addEventListeners and endEventListeners do I have to put
It’s not working
How is it not working? Are you getting errors?
Please describe what you expect, what you’re getting. Post some code. What have you tried?
Please help us help you. We can’t read your mind.
Rob
It works now but I still have the problem I just told you on the other topic .
I don’t know how to make it stop .
When you create a timer, you can store a handle to it in a variable:
local myTimer = timer.performWithDelay(1000, doSomething, 20)
Later if you want to cancel it:
timer.cancel( myTimer )
Rob
I put this and it’s still not working .
--this must NOT be local to the start game function, as you will need to access it in the exit game function local startTime --create a text object to display the timer local myTimeText = display.newText("0", 50, 50, native.systemFont, 20) local function startGame() --get time that the game started startTime = system.getTimer() end local function update() --get the current time local now = system.getTimer() --calculate time in milliseconds spent in game local timeSpentInGame = now - startTime --convert to seconds if that is useful for you local secondsInGame = timeSpentInGame / 1000 --update the text object to show the number of seconds myTimeText.text = secondsInGame end timer.cancel( myTimer ) startGame()
system.getTimer is not a timer.performWithDelay type timer. In fact Its difficult to call system.getTimer a timer at all. It’s simply the amount of time since your app started. It can’t be cancelled or reset. It will always be the time since your app started.
If you want to use it as a timer, you will be responsible to determine when to start and stop showing the time and when you start the timer, you will have to subtract the current value of system.getTimer from your captured value at start to get the difference from the start variable.
While you can do that, you’re doing too much work. Look up the docs for timer.performWithDelay() it’s better way to do timers and you can cancel them.
Rob
I tried but it’s very confusing
How do I stop it and reset it ?
To stop the code in my example, you would need to stop the event listener:
Runtime:removeEventListener("enterFrame", update)
To reset the timer, you would need to call startGame() again.
Where you call these functions is going to be entirely dependant on your app, and how you need things to happen.
If you have not done so already, I think that it would be wise to go over some of the Corona sample code and tutorials to give yourself a better idea of how timers and events both work:
https://coronalabs.com/resources/tutorials/getting-started-with-corona/
Where do i call the startgame() again ?
That depends on your game. All I’ve done is given you an example, where I call that function when I start the game.
We don’t know your game or project structure. Perhaps you have an “enterScene” function, maybe you are using Composer, maybe not?
You will need to work out where you should put that call so that it happens when you need it to.
How do I change the seconds I just want it to count numbers not seconds .
I don’t think he means a score counter, he specifically said he wants the time that the user was in the game.
brandont264, the easiest way I can think of is to record the time that the user starts the game, then when the game ends the length of time will be:
timeSpentInGame = endTime - startTime
I don’t know how your code is set out (e.g. if you are using scenes with enter/exit functions) but here is the gist of what to do to get the time spent in the level:
--this must NOT be local to the start game function, as you will need to access it in the exit game function local startTime local function startGame() --get time that the game started startTime = system.getTimer() end local function endGame() --get time that the game ended local endTime = system.getTimer() --calculate time in milliseconds spent in game local timeSpentInGame = endTime - startTime --convert to seconds if that is useful for you local secondsInGame = timeSpentInGame / 1000 end