Need help understanding timers for a game

Mark01970

New member
Joined
Aug 5, 2005
Messages
1
Programming Experience
Beginner
I am teaching my vb.net slowly but surely. I'm not having much luck determing how to properly use a timer.

Basically, to learn how to use a timer I am making a very simple little program, that gives and X-location and a Y-Location for "your ship" a SPEED and HEADING as well.

When you click on a forward button I want the x and y locations to change depending on your heading, and I want it to update this every 10th of a second or so.

So I am putting the form1_load into an infinite loop.

I am using the following code snippet I found to try and set this up, but am getting an error when I compile about WithEvents. The following snippet isn't MY program, it is just the only info I have found about these timers so far. If needed, I can post the source for my app. I just know next to nothing about the whole timer/tick stuff. Does anyone know of a tutorial someplace, or can anyone give me some help with this class??

Mark

-------

Private timecounter As Int32 = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Timer1.Interval = 1000 '1 second (timer works in millisecs.

End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

timecounter += 1

Label1.Text = Str(timecounter)

End Sub
 
Timers are fairly simple. If you're getting stuck in a loop, you have to put a count on it some how and after that count is reached, set the timer.enabled=false.. see below...

'CODE'

Public x As Int32
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If x = 10 Then Timer1.Enabled = False
Item.Left = (Item.Left + 10)
Item.Top = (Item.Top + 10)
x = x + 1
End Sub

'END CODE'

Hope this helps
 
Mongoose, not to be a brat but when posting code you can simply use the [ code] & [ /code] tags to denote code segments (minus the space right between the 1st & 3rd characters of course)

these tags also make it easier to read code on the forums and provides spacing for organization
 
Back
Top