0

I basically have a bunch of black boxes and I want the player (PictureBox1) to have gravity, fall, and hit the boxes so they have to jump over them .... I have basic motion of everything, just th "physics" that I need to code.

The Code below is running on a timer, so that it is always pushing the player down by 8, and if it intersects with a block, it starts to push the other way ....

I grouped all the "Blocks" with Blocks(49) = Picturebox .. then individually doing Blocks(1)= Pictuebox1 , Blocks(2) = Picturebox2. So on...

 PictureBox1.Top += 8

    For x As Integer = 1 To 1

        If PictureBox1.Bounds.IntersectsWith(floor(x).Bounds) And floor(x).Visible = True Then
            standing = True
            PictureBox1.Top -= 1
        End If
    Next x

    For y As Integer = 1 To 49
        If PictureBox1.Bounds.IntersectsWith(blocks(y).Bounds) And blocks(y).Visible = True Then
            standing = True
            PictureBox1.Top -= 1
        End If

    Next y
Ds.109
  • 714
  • 8
  • 16
  • 32
  • Please narrow down your problem and ask a specific question, for a specific part which you're having trouble with. You shouldn't expect anyone on Stack Overflow to watch youtube videos and analyze tons of code to answer a question. – Meta-Knight Dec 06 '12 at 15:06

1 Answers1

0

I grouped all the "Blocks" with Blocks(49) = Picturebox .. then individually doing Blocks(1)= Pictuebox1 , Blocks(2) = Picturebox2. So on...

I sure hope you don't do Blocks(1)= Picturebox1 because PictureBox1 is your player, not a block ;-) Also, arrays are 0-based in VB.NET so you should start at index 0. In your form, instead of naming your blocks PictureBox2, PictureBox3, etc. I would advise naming them something like BlockPicBox1, BlockPicBox2, etc. Then you could assign your array like this:

Dim Blocks As PictureBox() = {BlockPicBox1, BlockPicBox2, BlockPicBox3, BlockPicBox4, ...}

Now let's look at your code:

For x As Integer = 1 To 1
    ....
Next

This loop is wrong. You want to loop through all floors. Use the Count property to make sure you loop through all floors, or even better, use a For Each loop.

If PictureBox1.Bounds.IntersectsWith(floor(x).Bounds) And floor(x).Visible = True Then
    standing = True
    PictureBox1.Top -= 1
End If

The condition seems good, but why substract the Top property by -1? You want your character to stand on the floor, so set the Top property accordingly.

You should also work on your naming, it will help make your code more readable. For example, instead of PictureBox1, use PlayerPicBox. And use plural for arrays ("floors" instead of "floor"). That would give:

For Each currentFloor As PictureBox in floors
    If currentFloor.Visible AndAlso PlayerPicBox.Bounds.IntersectsWith(currentFloor.Bounds) Then
        standing = True
        PlayerPicBox.Top = currentFloor.Top - PlayerPicBox.Height
    End If
Next

The same principles apply for your second loop.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58