[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Scrollbar needs to be faster

Hi all,

I am programming a small game which uses a simple horizontal scrollbar to increase / decrease the speed of a character moving across the screen.  It works fine and will alter the speed slightly when at its maximum and minimum values, but I was wondering how to make it so that when the scrollbar is set to fast it moves very very quickly across the screen?

The scrollbar values are:

Maximum: 500
Minimum: 4
Value: 250

The character moves 10 pixels each timer tick.

Thanks,

John.
0
john-formby
Asked:
john-formby
  • 5
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You have two basic options:

A) Decrease the interval of your timer so it fires more often.
B) Increase the distance the character moves per timer tick.

When you change the scroll bar, which are you changing...distance moved or timer interval?

~IM
0
 
john-formbyAuthor Commented:
I am setting the timer interval to whatever the value on the position of the scrollbar is.

John.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So when you have the scrollbar set at 4, it is not quick enough for you?

...or are you simply trying to figure out how to invert the scrollbar so that it goes fastest when all the way to the right and slowest when all the way to the left?

The following HScrollBar has a min and max of 0 with the large and small change set to 1:

Public Class Form1
    Inherits System.Windows.Forms.Form

#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 HScrollBar1 As System.Windows.Forms.HScrollBar
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
        Me.Label2 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'HScrollBar1
        '
        Me.HScrollBar1.LargeChange = 1
        Me.HScrollBar1.Location = New System.Drawing.Point(16, 32)
        Me.HScrollBar1.Name = "HScrollBar1"
        Me.HScrollBar1.Size = New System.Drawing.Size(208, 16)
        Me.HScrollBar1.TabIndex = 1
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(208, 24)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Label2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.HScrollBar1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private min As Integer = 4
    Private max As Integer = 500

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

    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Label2.Text = CInt(max - (HScrollBar1.Value / HScrollBar1.Maximum) * (max - min))
    End Sub

End Class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
john-formbyAuthor Commented:
This is what I have at the moment:

    Private Sub hsbSpeed_Change()
        Timer1.Interval = hsbSpeed.Value
    End Sub

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

// Code that changes images

       End Sub

The maximum value of 500 is not quick enough for what I want :-(

John
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The timer interval is in milliseconds, so when it is at 500, the timer fires every 500 milliseconds, or every 1/2 second.  When it is at 4, it fires every 4 milliseconds, which is faster than firing every 1/2 second.

So an interval of 500 fires SLOWER than an interval of 4, which is why I don't understand your statement:

>> The maximum value of 500 is not quick enough for what I want :-(

If 500 is not quick enough, then lower your max value of your scrollbar.

~IM
0
 
john-formbyAuthor Commented:
In the properties box on the right hand side of the screen I have set the properties of my scrollbar to:

Maximum: 500
Minimum: 4
Value: 250

When the program is executed at runtime, and the scroller inside the scrollbar is at 500 (as far left as it can go) then the character is moving at its fastest.  When the scroller inside the scrollbar is at 4 (as far right as it can go) the character is moving at its slowest.

I want to know if there is anyway that I can make the character move faster than it does now when it reaches the far left of the scrollbar?

I don't think I have been explaining this very well, sorry

John
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok.  You must have the RightToLeft property set to Yes, making the maximum value on the left and the minimum value on the right.

But earlier you said:

>> I am setting the timer interval to whatever the value on the position of the scrollbar is.

So when the scrollbar is to the far left, it will have a value of 500 and you are then setting the timer interval to 500.

The larger the timer interval, the slower it will fire.  When the interval is at 500, you will get a tick 2 times every second.  When the interval is at 4, you will get a tick 250 times a second.

So the when the scrollbar is to the left, it should move slower than when it is to the right.

But this is the exact opposite of what you are describing so either I am missing something or you have left something out.

=)

Sorry for the confusion here...

~IM
0
 
john-formbyAuthor Commented:
Ok, I am very new to VB, I thought that the code:

    Private Sub hsbSpeed_Change()
        Timer1.Interval = hsbSpeed.Value
    End Sub

is setting the interval to whatever the value of the scrollbar is?  It changes speed faster and slower, I just want it to move quicker but it seems to have hit a limit.  I thought it might just be a case of adjusting one of the properties but I have tried them all with no luck.

John  
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You are right.  That will change the interval, and consequently the speed.  Based on your description, it should move faster when it is all the way to the right.

It will hit a limit eventually, depending on what you are doing in the timer event.  A timer firing faster than somewhere below 25 milliseconds will probably not see anymore improvement in speed.

If you need it to move faster than that then you need to either:

A) Reduce the amount of code in the Timer event
B) Make the character move farther each tick.

~IM
0
 
john-formbyAuthor Commented:
Thanks for your help, I think I shall make the character move further each tick :-)

John
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now