Movement problem-VBForums
Results 1 to 5 of 5

Thread: Movement problem

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    3

    Question Movement problem

    I have a code to move a bunch of arrays that are targets. this is the code

    Private Sub Timer1_Timer()
    Dim i
    For i = 0 To 9
    Target(i).Top = Target(i).Top - ((-1) ^ i) * Rnd(1) * speed
    Target(i).Left = Target(i).Left - ((-1) ^ i) * Rnd(1) * speed
    If Target(i).Top < 0 Or Target(i).Left < 0 Then
    Target(i).Top = Me.ScaleHeight
    Target(i).Left = Me.ScaleWidth
    End If
    If Target(i).Top > Me.ScaleHeight Or Target(i).Left > Me.ScaleWidth Then
    Target(i).Top = Me.Top
    Target(i).Left = Me.Left
    End If
    Next i
    End Sub

    But i have 1 problem. Every time the targets move to the top left of the screen, they would come out of the bottom right of the screen, and that is supposed to happen. BUT for some reason, when they go to the bottom right of the screen, they will come out of the top left ( like they should ) but they stay there frozen and flickering? please tell me if you know why.

    Thanks

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,354

    Re: Movement problem

    You must have something else in your code...that standolane code worked just fine in an example I did.
    Try that in a new project with nothing else, and see if you still experience the same issue.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,049

    Re: Movement problem

    What are the values of Me.Top and Me.Left?
    It seems odd to place the targets based on where the form is located on the desktop.
    Why not just set them to 0,0, which would be the opposite corner to scaleHeight and scaleWidth.

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    830

    Re: Movement problem

    Passel is right - you should be setting the control Top and Left properties to 0 when the control has moved off the bottom-right edge of the window.

    Also, you will get a smoother looking motion of controls moving outside the top-left edge of the window if you test against the bottom-right corner of the control instead of the top-left corner as follows:

    Code:
    Private Sub Timer1_Timer()
       Dim i As Long
    
       For i = Target.LBound To Target.UBound
          Target(i).Top = Target(i).Top - ((-1) ^ i) * Rnd(1) * speed
          Target(i).Left = Target(i).Left - ((-1) ^ i) * Rnd(1) * speed
          If (Target(i).Top + Target(i).Height) < 0 Or (Target(i).Left + Target(i).Width) < 0 Then
             Target(i).Top = Me.ScaleHeight
             Target(i).Left = Me.ScaleWidth
          ElseIf Target(i).Top > Me.ScaleHeight Or Target(i).Left > Me.ScaleWidth Then
             Target(i).Top = 0
             Target(i).Left = 0
          End If
       Next i
    End Sub

  5. #5
    Fanatic Member
    Join Date
    May 2014
    Location
    Preveza Greece
    Posts
    864

    Re: Movement problem

    Vvmichelvv
    Check the scalemode, and inform us

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.