time countdown 10 9 8 code :S

Monkey Forums/Monkey Programming/time countdown 10 9 8 code :S

GC-Martijn(Posted 2012) [#1]
I'm searching for a simple code to countdown x seconds.

What I try was running way to fast.
It must slow down, so people can see the seconds (as real seconds)

Field timer:Int=10 ' countdown from 10

Method OnUpdate:Int()
timer=timer-(Millisecs()/1000)

If timer<=0
' done 
EndIf

return 0
End Method

Method OnRender:Int()
DrawText("Time: "+timer,0,0)
End Method


Thanks !


Shinkiro1(Posted 2012) [#2]
Try making timer a float.


GC-Martijn(Posted 2012) [#3]
its a float now, but still very fast, the speed I need is like this (a second):




Fryman(Posted 2012) [#4]

Field CurrentTime:int
Field timer:int = 10

Method OnCreate()

CurrentTime = Millisecs()

End Method

Method OnUpdate()

If Millisecs()-CurrentTime > 1000 then 
CurrentTime = Millisecs()
timer = timer -1

End If
End Method





Something like this


Jesse(Posted 2012) [#5]
Or this
Strict
Import mojo

Function Main:Int()
	New Game
	Return True
End Function

Class Game Extends App
	Field delay:Int
	Field time:Int
	Field counter:Int

	Const SECOND:Int = 1000
	Method OnCreate:Int()
		delay = SECOND * 10 '10 seconds
		time = Millisecs()
		counter = ((time+delay) - Millisecs())/SECOND
		SetUpdateRate 30
		Return True
	End Method

	Method OnUpdate:Int()
		counter = ((time+delay) - Millisecs())/SECOND
		Return True
	End Method

	Method OnRender:Int()
		Cls
		If counter > 0 
			DrawText counter,50,50
		Else
			DrawText "Done",50,50
		Endif
		Return True
	End Method

End Class



NoOdle(Posted 2012) [#6]
Or this
Strict
Import mojo

Function Main : Int()
	New Game
	Return True
End Function


Class Game Extends App

	Field totalTime : Int = 10	
	Field startTime : Int
	Field currentTime : Int


	Method OnCreate : Int()
		SetUpdateRate 60
		
		'start the timer
		startTime = Millisecs()
		currentTime = totalTime
		
		Return True
	End Method


	Method OnUpdate : Int()

		'update the timer
		If currentTime > 0
			currentTime = totalTime - (( Millisecs() - startTime ) / 1000 )
		Endif

		Return True
	End Method


	Method OnRender : Int()
		Cls 0, 0, 0
		
		'draw the timer
		SetColor 255, 255, 255
		DrawText currentTime, 0, 0
		
		Return True
	End Method

End Class



Fryman(Posted 2012) [#7]
Oh and the problem with your code is you dont reset timer to 10 each time
onUpdate is run, so the -millisecs()/1000 is cumulative


GC-Martijn(Posted 2012) [#8]
how good is the support here ! 3 correct codes ! thanks its working now :)