|
-
Jun 25th, 2009, 03:58 PM
#1
Thread Starter
Addicted Member
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
-
Jun 25th, 2009, 04:49 PM
#2
Re: Time Counter
This will give you an idea of what you would need to do:
vb.net Code:
Public Class Form1 Public Sub New() InitializeComponent() Me.Timer1.Enabled = True Me.Timer1.Interval = 1000 End Sub Private time As New TimeSpan(0, 5, 0) Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick time = time.Subtract(New TimeSpan(0, 0, 1)) Me.Label1.Text = time.Minutes.ToString() & ":" & time.Seconds.ToString() End Sub 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.
-
Jun 25th, 2009, 04:53 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|