Results 1 to 3 of 3

Thread: Time Counter

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Time Counter

    I have recently started creating a basic game however i am having some trouble creating a time counter.

    Basically what i want is a counter which is a label displaying say "05:00" as in five minutes. Then when the level is started i want it to count down to zero and make my game over label visible.

    Any advice appreciated greatly

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Time Counter

    This will give you an idea of what you would need to do:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         InitializeComponent()
    5.         Me.Timer1.Enabled = True
    6.         Me.Timer1.Interval = 1000
    7.     End Sub
    8.  
    9.     Private time As New TimeSpan(0, 5, 0)
    10.  
    11.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    12.         time = time.Subtract(New TimeSpan(0, 0, 1))
    13.         Me.Label1.Text = time.Minutes.ToString() & ":" & time.Seconds.ToString()
    14.     End Sub
    15.  
    16. End Class

    The only thing is, you will have to run your timer on a separate thread so that the user can still play the game. Take a look at jmc's BackGroundWorker thread in the CodeBank.

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Time Counter

    Well you will need a timer to update the time elapsed, you could do something like this:

    Code:
        Dim StartTicks As Long
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            StartTicks = Now.Ticks
            Label1.Text = "5:00"
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim ts As New TimeSpan(0, 5, 0)
            Dim ElapsedTime As New TimeSpan(Now.Ticks - StartTicks)
            Dim tsout As TimeSpan = ts.Subtract(ElapsedTime)
            Label1.Text = tsout.Minutes.ToString() + ":" + tsout.Seconds.ToString("00")
            If tsout.TotalSeconds < 0 Then
                Timer1.Enabled = False
                Label1.Text = "0:00"
            End If
    
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width