Millisecs() based wait/timer. Help needed.

Monkey Forums/Monkey Programming/Millisecs() based wait/timer. Help needed.

Stuart(Posted 2014) [#1]
When my game ends based on the following conditions, ( player scores max points before opponent or player loses all lives or opponent scores max points before player ), I desire the computer to allow the user a certain length of time in which to press the spacebar to play again. If the user does not press the spacebar within the allotted interval then the game returns to the main menu.
What am I not doing correctly below?

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method GAME_OVER_Conditions:Int()
		If plr_pnts >= GAME_OVER_PNTS Then
			StopChannel ( 0 )
			PlaySound ( sfx_plr_wins , 5 )
			game_over_message = 1
			prev_millisecs = Millisecs()
			game_mode = 3
		Endif
		
		If plr_lives = 0 Then
			StopChannel ( 0 )
			PlaySound ( sfx_opnt_wins , 9 )
			game_over_message = 2
			prev_millisecs = Millisecs()
			game_mode = 3
		Endif
		
		If opnt_pnts >= GAME_OVER_PNTS Then
			StopChannel ( 0 )
			PlaySound ( sfx_opnt_wins , 9 )
			game_over_message = 3
			prev_millisecs = Millisecs()
			game_mode = 3
		Endif
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method Game_Over:Int()
		If KeyHit ( KEY_SPACE ) Then
			For Local i:Int = 0 To 12
				StopChannel (i)
			Next
			SetChannelVolume ( 0 , music_volume*0.25 )
			PlaySound ( music_in_game , 0 , 1 )
			PlaySound ( sfx_game_start , 2 )
			Reset_Game()
			game_mode = 1
		Endif
		
		EXIT_To_Main_Menu()
		
		If Millisecs() - prev_millisecs = 5000.0 Then game_mode = 0
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	



Wylaryzel(Posted 2014) [#2]
Whats behind the EXIT_To_Main_Menu() method/function?

And how is the selection for the different game_mode 's defined?

From that piece of code, I think we can't tell you whats wrong :-)

Br
Wyl


Jesse(Posted 2014) [#3]
it is impossible to tell what's wrong with your code from what you posted. Although, "EXIT_To_Main_Menu()" is indicative that you are going to the main menu as soon as you call "Game_Over()" but my guess is as good as any.


Shinkiro1(Posted 2014) [#4]
If Millisecs() - prev_millisecs = 5000.0 Then game_mode = 0

If you are updating your game at 60 frame per second, your game will advance around 16 milli-seconds each frame.
That means you got a pretty low chance for the difference to be exactly 5000. Check if it's greater than 5000.


Stuart(Posted 2014) [#5]
Thankyou, Shinkiro1, yours was the answer to my problem .... 'You Da Man!'


Stuart(Posted 2014) [#6]
One other question. How may I create a timer starting from 0 seconds displaying the number of seconds during the interval before the game returns the player to the main menu?
I know that dividing Millisecs() by 1000 produces seconds; I don't know how to reset Millisecs() to 0.


Supertino(Posted 2014) [#7]
I'd do something quick like this;

Global myTimer:Int
Global seconds:Int

Function CountSeconds:Void()
   If myTimer < Millisecs()
      myTimer = millisecs()+1000
      seconds = seconds+1
   Endif
End Function

Function DrawSeconds:Void(x:Int=0,y:Int=0)
   DrawText seconds,x,y
End function