Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Move Towards Mouse

  1. #1

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Resolved [RESOLVED] [2005] Move Towards Mouse

    How can I get a picturebox to move towards the mouse?
    I've tried the following code but it doesn't work properly.
    It doesn't go straight to the mouse(direction between points?) it either goes up/down or left/right until it reaches the mouse:

    I have 2 variables; 'mouseLeft' and 'mouseTop' that get the mouse position in the 'Main_MouseMove' event.

    Code:
            If mouseLeft > picMove.Left Then
                picMove.Left += 3
            End If
            If mouseLeft < picMove.Left Then
                picMove.Left -= 3
            End If
            If mouseTop > picMove.Top Then
                picMove.Top += 3
            End If
            If mouseTop < picMove.Top Then
                picMove.Top -= 3
            End If
    Thanks in advance, knxrb
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  2. #2

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005]Move Towards Mouse

    Bump
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Move Towards Mouse

    are you looking for animation here?

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] Move Towards Mouse

    Try this:

    Put a timer control and a picturebox on the form and this code:

    You can alter the MoveUnits and the Timer1.Interval to get the desired speed and tolerence.
    vb.net Code:
    1. Public Class Form1
    2.     Const MoveUnits As Integer = 5
    3.     Dim mouseX, mouseY As Integer
    4.  
    5.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    6.         Timer1.Interval = 5
    7.         Timer1.Enabled = True
    8.     End Sub
    9.  
    10.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    11.         mouseX = e.X
    12.         mouseY = e.Y
    13.     End Sub
    14.  
    15.     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    16.         Dim nX, nY As Integer
    17.         nX = PictureBox1.Left + PictureBox1.Width \ 2
    18.         nY = PictureBox1.Top + PictureBox1.Height \ 2
    19.         If mouseX > nX Then nX += MoveUnits Else nX -= MoveUnits
    20.         If mouseY > nY Then nY += MoveUnits Else nY -= MoveUnits
    21.         If Math.Abs(mouseX - nX) > MoveUnits Then PictureBox1.Left = nX - PictureBox1.Width \ 2
    22.         If Math.Abs(mouseY - nY) > MoveUnits Then PictureBox1.Top = nY - PictureBox1.Height \ 2
    23.     End Sub
    24. End Class

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Move Towards Mouse

    [EDIT] How would I get the form to paint a line to where it is going next?
    Example: the picturebox is going left so the form will draw a line to where the picturebox will change direction left until it changes direction. (See pic below for better example)


    [EDIT]


    kleinma: Not really animation, I just want to have a control move towards my mouse. I think Pradeep has solved it now, thanks anyway

    Pradeep1210: Thanks, it works better than mine
    Last edited by knxrb; Nov 26th, 2008 at 01:45 PM.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] Move Towards Mouse

    I can't understand what you are trying to achieve.
    Is the point of meeting of blue and red lines the current mouse position?
    Should those lines always be perpendicular to each other?
    what is the circle (and the rectangle which I assume is the picturebox) in that drawing?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Move Towards Mouse

    It's alright I've sorted it now:

    Code:
            e.Graphics.DrawLine(Pens.Green, picPixel.Left, picPixel.Top, picPixel.Left, picMove.Top)
            e.Graphics.DrawLine(Pens.Green, picMove.Left, picMove.Top, picPixel.Left, picMove.Top)
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

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