Prevent Sprite from escaping Form

Cheetaiean

Member
Joined
Jan 20, 2014
Messages
13
Programming Experience
Beginner
So I have a skier which I control with the arrow Keys.

To make him move I use Booleans (ex.GoRight, GoLeft). When trying to prevent him from leaving the form I use things like

VB.NET:
If (Me.imgSki.Top - 10) < 0 Then            GoUp = False
        ElseIf (Me.imgSki.Top - 10) > 0 Then
            GoDown = False
        End If

However, that does not prevent him from getting repelled or stopped by the border, only if i stop pressing the GoRight key for example will the skier stop.

Any help would be greatly appreciated.
 
Okay update...
I got the borders to work, but only on the Left and Top side...

VB.NET:
If GoRight = True Then            imgSki.Left += Snowball
        End If
        If GoLeft = True Then
            imgSki.Left -= Snowball
        End If
        If GoUp = True Then
            imgSki.Top -= Snowball
        End If
        If GoDown = True Then
            imgSki.Top += Snowball
        End If
        If GoRight = True Then


            imgSki.Left += Snowball
        End If


        If (imgSki.Left - 10) < Me.ClientRectangle.Left Then
            BorderX = True


        End If
        If (imgSki.Left - 10) > Me.ClientRectangle.Left Then
            BorderX = False
        End If


        If BorderX = True Then
            imgSki.Left += 20
        End If


        If (imgSki.Top + 10) < Me.ClientRectangle.Top Then
            BorderY = True
        End If
        If (imgSki.Top + 10) > Me.ClientRectangle.Top Then
            BorderY = False
        End If
        If BorderY = True Then
            imgSki.Top += 20
        End If
Just need to know how to have the same happen on the Bottom and Right.
 
The code would be basically the same except that you would use Bottom instead of Top, Right instead of Left and reverse the < and > operators and the + and - operators.
 
Thanks a lot! I didn't realize that the Client Rectangle could have a .Right and .Top; I thought it followed the rules of the sprite (which only has .Left and .Top).
 
Back
Top