# Solved: VB . Net Increasing timer tick rate



## dis0wned1 (Mar 14, 2008)

Im using a timer to maintain the speed at which an object moves and as part of the program the speed of the object needs to slowly increase and then once a certin point is reached stop accelerating. this is for a programing assignment and must use the timer.
Any help would be apreciated here is the code i have so far it works in that the vehcle travels at a constant speed.
Imports System.Math

Public Class Form1
Inherits System.Windows.Forms.Form
Dim Direction As Double = 0

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer. 
'Do not modify it using the code editor.
Friend WithEvents imlRallyCar As System.Windows.Forms.ImageList
Friend WithEvents tmrMove As System.Windows.Forms.Timer
Friend WithEvents picCar As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.imlRallyCar = New System.Windows.Forms.ImageList(Me.components)
Me.tmrMove = New System.Windows.Forms.Timer(Me.components)
Me.picCar = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'imlRallyCar
'
Me.imlRallyCar.ImageSize = New System.Drawing.Size(48, 48)
Me.imlRallyCar.ImageStream = CType(resources.GetObject("imlRallyCar.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.imlRallyCar.TransparentColor = System.Drawing.Color.FromArgb(CType(97, Byte), CType(68, Byte), CType(43, Byte))
'
'tmrMove
'
Me.tmrMove.Enabled = True
'
'picCar
'
Me.picCar.Location = New System.Drawing.Point(88, 96)
Me.picCar.Name = "picCar"
Me.picCar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
Me.picCar.TabIndex = 0
Me.picCar.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.picCar)
Me.Cursor = System.Windows.Forms.Cursors.Cross
Me.Name = "Form1"
Me.Text = "Follow Me"
Me.ResumeLayout(False)

End Sub

#End Region
Private Sub Form1_load(ByVal sender As Object, ByVal Byvale As System.EventArgs) Handles MyBase.Load
'Start with the car in the 0 direction
picCar.Image = imlRallyCar.Images.Item(0)
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
'Calculate The direction of the car from the cursor
Direction = DirnDeg(picCar, New Point(e.X, e.Y))
End Sub
Private Sub tmrMove_tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrMove.Tick
'calculate and load the correct image
Dim WhichImage As Integer = CInt(Direction / 360 * 32) Mod 32
picCar.Image = imlRallyCar.Images.Item(WhichImage)
'use cos and sin to calculate the change in location
picCar.Left += 4 * Cos(Direction / 180 * PI) 'Angle in radians
picCar.Top += 4 * Sin(Direction / 180 * PI) 'Angle in radiuns
End Sub
End Class


----------



## Chicon (Jul 29, 2004)

Hi dis0wned1,

You may try this : while handling a tick of the timer in *tmrMove_tick*, you may stop the timer, reset its interval time and then restart it.
(More info about timer : here).

PS. I'm not a VB developer but I've already used similar objects in Java. They look to work the same way.

Edit :
----
Personally, I don't think it's a good thing to alter the interval time of a timer.
Your application may look choppy as the tick event is used by the system to refresh the form and its components.

There's a more elegant solution without modifying the interval time that will make your application run smoothly :
for example, if you want an accelerating effect by 2 :

- First, display the initial situation,
- Built your timer with an interval time set to 4 milliseconds and start it,
- skip 255 ticks,
- Tick : recompute the position of your car,
- skip 127 ticks,
- Tick : recompute the position of your car,
- skip 63 ticks,
- Tick : recompute the position of your car,
....
....
- Last tick : stop the timer when the number of ticks to skip is equal to 1.


----------



## dis0wned1 (Mar 14, 2008)

Solved
tmrMove.interval = tmrMove.interval - 1
thanks for your help anyway chicon and this tasks requirements were to use timers to perform this task i was told i must only use two timers to achieve it.


----------

