Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Game Timer
Started by scuba_games Sep 28 2019 09:57 AM

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

scuba_games

[GLOBAL: userInfoPane.html]
scuba_games
  • Enthusiast

  • 31 posts
  • Corona SDK

I'd like to make a timer for game play, then have it log to a leaderboard.

 

I can make get minutes and seconds to work, but not milliseconds.

 

Any help would be great

 

timer_help.jpg

 

local function updateTime( event )
   gameText.counterTenths = gameText.counterTenths + 1
   gameText.minutes = math.floor( gameText.counterSeconds / 60 )
   gameText.seconds = math.floor( gameText.counterTenths / 100 )
   gameText.tenths = gameText.counterTenths % 100
     
   gameText.timeDisplay = string.format( "%02d:%02d:%02d", gameText.minutes, gameText.seconds, gameText.tenths )
 
   gameText.clockText.text = gameText.timeDisplay
end
 
 
misc.counterTimer = timer.performWithDelay(1,updateTime, 0)



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,680 posts
  • Enterprise

It's not going to work either. Corona only updates the screen ever 1 / fps seconds. So if you're running at 60 fps, you will get an update every 16.67ms. Any value to timer.performWithDelay() less than 17 isn't going to fire multiple times.

 

The best way to do this will involve an enterFrame listener, and use system.getTimer() (http://docs.coronalabs.com/api/library/system/getTimer.html) which has millisecond resolution.  You get the time from that and update your values. They will still only update once a frame, but you're getting millisecond values to update your tenths with.

 

Rob



[TOPIC: post.html]
#3

scuba_games

[GLOBAL: userInfoPane.html]
scuba_games
  • Enthusiast

  • 31 posts
  • Corona SDK

It's not going to work either. Corona only updates the screen ever 1 / fps seconds. So if you're running at 60 fps, you will get an update every 16.67ms. Any value to timer.performWithDelay() less than 17 isn't going to fire multiple times.

 

The best way to do this will involve an enterFrame listener, and use system.getTimer() (http://docs.coronalabs.com/api/library/system/getTimer.html) which has millisecond resolution.  You get the time from that and update your values. They will still only update once a frame, but you're getting millisecond values to update your tenths with.

 

Rob

Thank you Rob.

 

I am getting warmer.  Here is what I have, but my math is definitely wrong.

 

 
misc.x = system.getTimer()
misc.b = nil
 
local function updateTime( event )

    misc.b = (system.getTimer() - misc.x)

    gameText.minutes = math.floor( misc.b / 60 )
    gameText.seconds = math.floor( misc.b / 1000 ) 
    gameText.tenths  = misc.b pick  1000
 
    gameText.timeDisplay = string.format( "%02d:%02d:%03d", gameText.minutes, gameText.seconds, gameText.tenths )
 
    gameText.clockText.text = gameText.timeDisplay
end
 

local function printTimeSinceStart( event )
    updateTime()
end
 
Runtime:addEventListener( "enterFrame", printTimeSinceStart )



 



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,680 posts
  • Enterprise

 gameText.tenths  = misc.b pick  1000

Isn't valid Lua.



[TOPIC: post.html]
#5

scuba_games

[GLOBAL: userInfoPane.html]
scuba_games
  • Enthusiast

  • 31 posts
  • Corona SDK

 gameText.tenths  = misc.b pick  1000

Isn't valid Lua.

 

Not sure why the word "pick" is there.

 

 

 
misc.x = system.getTimer()
misc.b = nil
 
local function updateTime( event )

    misc.b = (system.getTimer() - misc.x)

    gameText.minutes = math.floor( misc.b / 60 )
    gameText.seconds = math.floor( misc.b / 1000 ) 
    gameText.tenths  = misc.b % 1000
 
    gameText.timeDisplay = string.format( "%02d:%02d:%03d", gameText.minutes, gameText.seconds, gameText.tenths )
 
    gameText.clockText.text = gameText.timeDisplay
end
 

local function printTimeSinceStart( event )
    updateTime()
end
 
Runtime:addEventListener( "enterFrame", printTimeSinceStart )



 

  1.  

    Thank you Rob.

     

    I am getting warmer.  Here is what I have, but my math is definitely wrong.

     

     

     gameText.tenths  = misc.b pick  1000
    

    Isn't valid Lua.

     

     

     



[TOPIC: post.html]
#6

scuba_games

[GLOBAL: userInfoPane.html]
scuba_games
  • Enthusiast

  • 31 posts
  • Corona SDK

gameText.minutes = math.floor( misc.b / 60000 )
gameText.seconds = (math.floor( misc.b / 1000 ) ) % 60
gameText.tenths  = misc.b % 1000
 
I needed the "% 60" on the tail of gameText.seconds.
 
I have no idea what that % actually means, but in mind it's the value at which it starts over?


[TOPIC: post.html]
#7

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,974 posts
  • Corona SDK

% is the mod command. Essentially what it gives you is the remainder after the number on the left is divided by the number on the right.

 

So:

 

4 % 2 = 0

5 % 2 = 1

520 % 250 = 20



[TOPIC: post.html]
#8

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

The value for milliseconds that you are looking for is:

gameText.tenths = math.fmod(misc.b, 1000)


[TOPIC: post.html]
#9

scuba_games

[GLOBAL: userInfoPane.html]
scuba_games
  • Enthusiast

  • 31 posts
  • Corona SDK

Thank you all!  I think I'm good to go now.

 

Anyone ever dump "scores" in to a local leaderboard? 

 

I'm looking to make a scrollable list of names with times.




[topic_controls]
[/topic_controls]