Wait()

Lesther

New Member
Joined
Aug 3, 2018
Likes
0
#1
Hello. I am trying to make a game and I came across a problem that I do not know how to fix in cerberus x.Do you guys have any code for something that waits for n second and do an action. For example:

Wait 1 sec
Rnd(0,100)

I tried doing using the Millisecs() but I couldn’t make it work.
P.s I am still a beginner at this language
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
Likes
192
#2
To do it with Millisecs, you have to have a variable for storing the last time that Millisecs was used, call it lastTick.
You then use Millisecs in the OnUpdate method to get the current time and compare it to the lastTick+1000.
If the current time is greater or equal to lastTic+1000, you update lastTick with the current Millisecs.

Cerberus X:
Strict

Import mojo


Class CGame Extends App

    Field lastTick:Int
    Field counter:= 60    ' Sixty seconds
    
    
    Method OnCreate:Int()
        SetUpdateRate(60)
        Return 0
    End Method
    
    Method OnUpdate:Int()
        Local tick:= Millisecs()
        If tick >= Self.lastTick+1000 And counter>-1
            Self.counter -= 1
            Self.lastTick = tick
        Endif
        
        Return 0
    End Method
    
    Method OnRender:Int()
        Cls
        If Self.counter > -1 DrawText("Count down: "+Self.counter, 100, 100) Else DrawText("Count down finished", 100, 100)
    
        Return 0   
    End Method

End Class

Function Main:Int()
    New CGame()
    
    Return 0
End Function
 

Lesther

New Member
Joined
Aug 3, 2018
Likes
0
#3
I
To do it with Millisecs, you have to have a variable for storing the last time that Millisecs was used, call it lastTick.
You then use Millisecs in the OnUpdate method to get the current time and compare it to the lastTick+1000.
If the current time is greater or equal to lastTic+1000, you update lastTick with the current Millisecs.

Cerberus X:
Strict

Import mojo


Class CGame Extends App

    Field lastTick:Int
    Field counter:= 60    ' Sixty seconds
   
   
    Method OnCreate:Int()
        SetUpdateRate(60)
        Return 0
    End Method
   
    Method OnUpdate:Int()
        Local tick:= Millisecs()
        If tick >= Self.lastTick+1000 And counter>-1
            Self.counter -= 1
            Self.lastTick = tick
        Endif
       
        Return 0
    End Method
   
    Method OnRender:Int()
        Cls
        If Self.counter > -1 DrawText("Count down: "+Self.counter, 100, 100) Else DrawText("Count down finished", 100, 100)
   
        Return 0  
    End Method

End Class

Function Main:Int()
    New CGame()
   
    Return 0
End Function
Thank you for this. You have just solved a lot of problems that I have with my game.
 
Top Bottom