0
\$\begingroup\$

In my computer programming class we are creating our own top down adventure games in Visual Basic. Currently I have been able to have the player appear on screen and collide with a block on the screen.

My question comes from the collision code which is here:

Public Sub checkCollide()
        'If player's top is in range of the block above it...
        If yPos = Form1.block1Y + 49 And (xPos >= Form1.block1X And xPos <= Form1.block1X + 49 Or xPos + 49 <= Form1.block1X + 49 And xPos + 49 >= Form1.block1X) Then
            'Disable upward movement
            canMoveUp = False

        Else
            canMoveUp = True
        End If

        'If player's left is in range of the block to its left...
        If xPos = Form1.block1X + 49 And (yPos >= Form1.block1Y And yPos <= Form1.block1Y + 49 Or yPos + 49 <= Form1.block1Y + 49 And yPos + 49 >= Form1.block1Y) Then
            'Disable left movement
            canMoveLeft = False

        Else
            canMoveLeft = True
        End If

        'If the player's underside is in range of the block below it...
        If yPos + 24 = Form1.block1Y And (xPos >= Form1.block1X And xPos <= Form1.block1X + 49 Or xPos + 49 <= Form1.block1X + 49 And xPos + 49 >= Form1.block1X) Then
            'Disable downward movement
            canMoveDown = False

        Else
            canMoveDown = True
        End If

        'If the player's right is in range of the block to the right of it...
        If xPos + 24 = Form1.block1X And (yPos >= Form1.block1Y And yPos <= Form1.block1Y + 49 Or yPos + 49 <= Form1.block1Y + 49 And yPos + 49 >= Form1.block1Y) Then
            canMoveRight = False

        Else
            canMoveRight = True
        End If
End Sub

I was wondering if there was a way to create a struct or class of some sort that allowed for a much simpler way to do this. Each block is a picture box. When creating a new block I need to repeat these steps for each one created, resulting in a huge list that is probably unnecessary. Thanks.

\$\endgroup\$
0
\$\begingroup\$

Have you considered making an array of those PictureBox-es? But, to set the array one by one to an existing object is a pain for sure. You might want to initialize those objects by code. This might help

If you see in those example codes:

Dim x(10) As widget
// x now contains 11 elements of type widget, x(0) through x(10).

That is how you initialize an array.

// Assign a reference to an existing object to two array elements.

x(0) = specialWidget
x(1) = specialWidget

That is how you set an array one by one to an existing object. Pain (At least from my experience, i have to set 65 objects one by one).

// Create each element of an array by using a loop.
For q As Integer = 0 To 10
    x(q) = New widget()
Next q

And that is how you can by a way set the array with a new objects. You can set the position and everything else (initializing, like the position, width, and height of the objects) normally.

Also, there's this thread too about using List(Of T). It seems that it doesn't have to set the size of the array, so it should be more dynamic.


Oh, and for the collision check. You can do it like this pseudocode:

For i As Integer = 0 To x.Length
    // Check collision, to access the array use x(i)
Next

*I hope i get that right from here

\$\endgroup\$
  • \$\begingroup\$ Thanks. Didn't think of the array. I'll be able to look into it within the next hour. I should be able to get back to you sometime this week. \$\endgroup\$ – user220631 May 17 '16 at 21:08
  • \$\begingroup\$ Just tried it with an array. I was able to simplify it greatly. I used a for loop to iterate through every element and check for collision each time a step is taken. The only downside is that with the code before, it would let me pass through like normal because of the else statements. The only way I can think of for fixing it would be to go through each block and do the same thing but reverse it (If that makes any sense to you). \$\endgroup\$ – user220631 May 17 '16 at 21:56
  • \$\begingroup\$ @user220631 For that, you probably want to set all the canMoveXXXX to true before of the iteration (before the for loop) and then put it into false if you collide with a block (reverse thing you said?). To make it a little bit faster, in the beggining of the for loop (inside) check whether or not the canMoveXXXX is false, if it really is false then return. This way it won't check canMoveXXXX anymore because it already collided. \$\endgroup\$ – Greffin28 May 18 '16 at 1:37
  • \$\begingroup\$ Oops, for the check with return, might want to do it this way instead: surround the ifs of canMoveXXXX with If canMoveXXXX = True Then, that way it won't perform that ifs check again if it is already false or collide with another block. Well, we can't see the improvement that much though because it's not much. \$\endgroup\$ – Greffin28 May 18 '16 at 6:19
  • \$\begingroup\$ Collision is fixed and greatly simplified. I just added the canMoveXXXX = True statements every time checkCollision() is called. Thanks Greffin28. \$\endgroup\$ – user220631 May 19 '16 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.