Results 1 to 6 of 6

Thread: PictureBoxes Overlapping

  1. #1

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    PictureBoxes Overlapping

    Hi!
    I am creating a game where there are trucks moving right (TrucksR()) and trucks moving left (TrucksL()). After going off the screen, they reappear on the other side of the screen as a randomly selected truck. There are different trucks with different widths and when I try randomizing the images, eventually, the trucks start to overlap. The trucks pile on each other. How do I avoid this?
    Thanks in advance.
    Code:
    Private Sub movetruck()
            For i = 0 To 9
                If TrucksR(i).Left < 900 Then
                    TrucksR(i).Left += 5
                Else
                    Randomize()
                    TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
                    TrucksR(i).Left = -1 * TrucksR(i).Width
                End If
                If TrucksL(i).Left > -1 * TrucksR(i).Width Then
                    TrucksL(i).Left -= 5
                Else
                    Randomize()
                    TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
                    TrucksL(i).Left = 900
                End If
            Next
        End Sub

  2. #2
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: PictureBoxes Overlapping

    Try this.
    vb.net Code:
    1. Private Sub movetruck()
    2.         For i = 0 To 9
    3.             If TrucksR(i).Left < 900 Then
    4.                 TrucksR(i).Left += 5
    5.             Else
    6.                 Randomize()
    7.                 TrucksR(i).Image = Nothing
    8.                 TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
    9.                 TrucksR(i).Left = -1 * TrucksR(i).Width
    10.             End If
    11.             If TrucksL(i).Left > -1 * TrucksR(i).Width Then
    12.                 TrucksL(i).Left -= 5
    13.             Else
    14.                 Randomize()
    15.                 TrucksL(i).Image = Nothing
    16.                 TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
    17.                 TrucksL(i).Left = 900
    18.             End If
    19.         Next
    20.     End Sub

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: PictureBoxes Overlapping

    Maybe the bug is in this line
    Code:
    If TrucksL(i).Left > -1 * TrucksR(i).Width Then
    Shouldn't you check against TrucksL(i).Width not TrucksR(i).Width?



  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: PictureBoxes Overlapping

    You definitely need to get those Randomize calls out of there. Rnd and Randomize are holdovers from VB6, but calling Randomize inside a loop causes bad things to happen. What Randomize does is seeds the random number generator behind Rnd with the current system time down to the second. Two random number generators that get the same seed produce identical sequences. By calling Randomize in a tight loop, as you are doing, you will almost certainly be calling it twice in the same second, which will mean that you'll be getting the same number over and over from Rnd, rather than a new random number.

    If you use Randomize and Rnd, then you have to be sure that you don't call Randomize more than once in any given second. The easiest way to do that is to only call it once...ever....in the program. Better would be to switch to using the Random object, which replaced Randomize and Rnd in .NET, and is more intuitive to use. However, even with Random, you need to only create one Random object and use it everywhere, because creating a Random object has the same effect as calling Randomize, so you need to ensure that you don't create more than one a second, and you never need more than one anyways, so just create one.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: PictureBoxes Overlapping

    While all your suggestions have helped the trucks from not colliding, there are still a few instances of trucks on top of each other. Here is my code now.
    Code:
    Private Sub movetruck(ByVal Rand As Integer)
            For i = 0 To 9
                If TrucksR(i).Left < 864 Then
                    TrucksR(i).Left += 5
                Else
                    TrucksR(i).Image = Nothing
                    TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(Rand) & ".bmp")
                    TrucksR(i).Left = -1 * TrucksR(i).Width
                End If
                If TrucksL(i).Left > -1 * 50 Then
                    TrucksL(i).Left -= 5
                Else
                    TrucksL(i).Image = Nothing
                    TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(Rand) & ".bmp")
                    TrucksL(i).Left = 864
                End If
            Next
        End Sub

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: PictureBoxes Overlapping

    I would think instead of placing a truck to start at the edge of the screen when you generate a new truck, you should start it some random distance from the end of the truck in front of it, so that it won't overlap. You can double check the distance calculated to ensure it is off screen so it doesn't pop partially on the screen initially and fall back to positioning at the screen edge in that case.
    Last edited by passel; Jun 12th, 2017 at 10:24 AM.

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